1

My problem is how JQuery knows when to receive data, when I watch the browser's console I see that GET call is first and then the POST call is second. It should be POST first, then GET.

I tried solving my problem with .ready,and with some IF statements and so on but I still have not achieved what I wanted to achieve. Should I use some events?

My Javscript:

 (function($) {
   $(document).ready(function() {
     $("form#userdata").submit(function(){
       $.post( "savedata.php", $("form#userdata").serialize())
         .done(function( data ) {
           alert( "Send data" + data );
        });
       return false;
       alert("Error by passing data to php");
     });

 })})(jQuery);

 $.when($.ajax("savedata.php")).then(function(){
     $.get("savedata.php", function(data){
            alert("Php returns validation errors:+data);
     });
 });

My php script:

 // Get POST data
 $postData = $_POST;

 // Print out for testing
 // print_r($postData);

 // Read data
 $fistname = $_POST['firstname'];
 $surname=$_POST['lastname'];
 $email=$_POST['email'];

 // VALIDATION


 // Build return array and return JSON
 $returnData = $postData;

 //print(json_encode($returnData));
 echo json_encode($returnData);
 ?>
7
  • 1
    Did you even try to see your browser console?? Commented Oct 10, 2013 at 17:25
  • I am watching all the time.. Commented Oct 10, 2013 at 17:27
  • You didn't get a syntax error because of the missing doublequote? Was that just a copying error when you copied to SO? Commented Oct 10, 2013 at 17:27
  • 1
    You are mistaken if you think that you need POST (to send data) and another GET (to get data). Everything could easily be achieved using one POST or GET call to the script. You can directly return (echo/print) the result with this one call Commented Oct 10, 2013 at 17:29
  • 1
    Alexandre Wiechers Vaz no! If the validation is fine I will only put on page that new costumer has been add to mysql if there is validation error then I return zeros or ones. I return something like tihs Array={1,0,1,0,1,1,1}. So lets say that "1" means that input data is fine, if it's "0" that means that user did't complited that field or the value if filed isn't corrent(you cant pust your name Alexandre45. Am I clear?ž Commented Oct 10, 2013 at 17:36

3 Answers 3

2

$.get is called unconditionally, while the page is loading, because you didn't put it in an event handler.

$.post is only called when you submit the #userdata form, because it's called from the .submit() event handler.

Sign up to request clarification or add additional context in comments.

Comments

1

You can try something like this:

PHP:

// Get POST data
 $postData = $_POST;

 // Print out for testing
 // print_r($postData);

 // Read data
 $fistname = $_POST['firstname'];
 $surname=$_POST['lastname'];
 $email=$_POST['email'];

 // VALIDATION

 if(//validationError){
    echo json_encode(array('error' => //Your error message here));
    exit();
 }
 $returnData = $postData;

 //print(json_encode($returnData));
 echo json_encode(array('success' => $returnData));
 ?>

Then...

JS:

(function($) {

   $(document).ready(function() {

     $("form#userdata").submit(function(){

       $.post("savedata.php", $("form#userdata").serialize())
        .done(function( data ) {
           if(data.error)
              alert("Validation error: " + data.error);
           else
              alert( "Send data" + data.success );
        })
        .fail(function(){
           alert("Error by passing data to php");
        });
     });

 })})(jQuery);

13 Comments

Thx for your post! Oke I use your post as strucutre, but know says error ba passing data to php, now what should i do?
Take a look at your console "Network" tab and see what is the error returned by the ajax call!
It says status canceled, type pending, initator jq...js:8706 script and other unimportant data.
Ok, if it's saying "Error by parsing data", something is going wrong in your ajax call. It probably throws an error on console (One 4xx o4 5xx. See: en.wikipedia.org/wiki/…). What is the error??
Ok the post is fine 200, but I get error NS_ERROT_NOT_AVAILABLE: prompt aboted by user nsPrompter.js:440 (did't click any popup), and there is syntaxerror using //@ to indicate source map url pragmas is cepreceted user //# instead. jquery.min.js:1
|
1

You have your script incorrect

(function($) {
   $(document).ready(function() {
     $("form#userdata").submit(function(){
       $.post( "savedata.php", $("form#userdata").serialize())
         .done(function( data ) {
           alert( "Send data" + data );
        });
       return false;
       alert("Error by passing data to php");
     });

 })})(jQuery);

 $.when($.ajax("savedata.php")).then(function(){
     $.get("savedata.php", function(data){
            alert("Php returns validation errors:"+data); // You did not close the string literal and it would throw an error
     });
 });

2 Comments

Please compare the line with the last alert with your script and you'll easily see
He showed how it should be: he added a doublequote after validation errors:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.