1

I have an JQuery function which submits data from a php form to a different php file(submitForm.php). The submission works fine and there isn't any problem whatsoever, below is the handler:

    submitHandler : function(){                     
        $.ajax({  
            type: "POST",
            cache:false,  
            url: "submitForm.php",  
            data: $('#form').serialize(),
            success: function(data) {                       

            } 
        });
    }

Now, I want to be able get data from the submit form (submitForm.php), and load it into a different page.

This is my submitForm.php

   <?php
       $name="Amanda";
       $age="23";

       $data = array("name"=>"$name","age"=>"$age");
        header("Content-Type: application/json");
        echo json_encode($data);
   ?>

This is how my new submitHandler looks like

     submitHandler : function(){                     
    $.ajax({  
        type: "POST",
        cache:false,  
        url: "submitForm.php",  
        data: $('#form').serialize(),
        success: function(data) {                       
               var name= html(name);
               var age=html(age);

         $("#message").load("newpage.php",{name:name,age:age});

        } 
    });
}

I think I am doing it wrong, I would be grateful if somebody could correct me or give an idea as to how to do this. Thanks

3
  • Is this "new page" a new window or tab? or you mean use the returned data from submitForm.php to be displayed in the element $("#message")? Commented Oct 4, 2016 at 16:10
  • @DIEGOCARRASCAL It is using the returned data for another sql query, which is displayed in the element $("#message") Commented Oct 4, 2016 at 16:14
  • Then you shouldn't have any problem just displaying it, Hakan SONMEZ answer points the error in your code. Commented Oct 4, 2016 at 18:12

1 Answer 1

3

It should be like this. If you want to take your returner data you should use formal parameter of success function.

submitHandler : function(){                     
    $.ajax({  
        type: "POST",
        cache:false,  
        url: "submitForm.php",  
        data: $('#form').serialize(),
        dataType : 'json',
        success: function(data) {                       
               var name= data.name;
               var age=data.age;

         $("#message").load("newpage.php",{name:name,age:age});

        } 
    });
}

Edit: Also you need dataType: 'json' line.

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

1 Comment

Thanks @Hakan SONMEZ, I am grateful

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.