George
George

Reputation: 1114

Get data from a different php file using JQuery

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

Upvotes: 1

Views: 629

Answers (1)

Hakan SONMEZ
Hakan SONMEZ

Reputation: 2226

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.

Upvotes: 3

Related Questions