How to display a warning when a form is submitted successfully using PHP and jQuery?

jQuery modal runs fine, but I don’t know what I am missing when I want to display a warning message when the form has successfully completed php?

$.post( 'name.php', { ime: ime, location: location }, function(data){ if (data.success) { alert("form posted!"); } else { $("#dialog-form").dialog("open"); } }, "json" ); 

==========================

 if ($result == false) { echo json_encode(array('success' => false, 'result' => 0)); exit; } echo json_encode(array('success' => true, 'result' => $result)); $sql2 = "DELETE FROM $names WHERE name='$ime'"; $result2 = mysql_query($sql2); 
+4
source share
2 answers

It appears that the value of data.success not returning as true or false (or evaulating as boolean as you expect).

Try:

 $.post( 'name.php', { ime: ime, location: location }, function(data){ if (data.success == 'true') { alert("form posted!"); } else { $("#dialog-form").dialog("open"); } }, "json" 

);

You can also use FIDDLER to see the value returned from the server.

http://www.fiddler2.com/fiddler2/

+2
source

success has 3 arguments success(data, textStatus, jqXHR)

I believe you may want textStatus

 .success: 

The function to call when the request completes successfully. The function receives three arguments: the data returned from the server is formatted according to the dataType parameter; a string describing the status ; and jqXHR object (in jQuery 1.4.x, XMLHttpRequest).

 $.post( 'name.php', { ime: ime, location: location }, function(data, textStatus, jqXHR){ if (textStatus == "success") { alert("form posted!"); } else { $("#dialog-form").dialog("open"); } }, "json" ); 
+1
source

Source: https://habr.com/ru/post/1438656/


All Articles