0

I am fiddling with jQuery.ajax() and php, and I need some pointers in order to make everything work:

Here is the php code:

if(!empty($_POST["fname"])){
        $firstName = $_POST["fname"];
        echo $firstName."<br />";
    }
    if(!empty($_POST["id"])){
        $age = $_POST["id"];
        echo $age;
    }

Here is the jQuery code:

jQuery("#ajaxForm").submit(function(event){
    event.preventDefault();

    var firstName = jQuery("#firstName").val();
    var age = jQuery("#age").val();

    // jQuery.ajax() - Perform an asynchronous HTTP (Ajax) request.
    jQuery.ajax({
        type: "POST",
        url: "http://localhost/profiling/index.php",
        data: {fname:firstName, id:age}
    }).done(function(result){
        alert("Your data has been submitted!" + firstName);
    });
    var result;
    console.log(result);

});

The values from jQuery exist, I get the alert, telling me the data has been submitted, firebug shows the Ajax post as working.

Why doesn't php gets my data and echo it?

5
  • Have you tried console.log(result);? Commented May 8, 2013 at 23:18
  • 'result' will hold the data returned by the php. Commented May 8, 2013 at 23:20
  • because you're not doing it. Recieve date from that php and append it in some div Commented May 8, 2013 at 23:22
  • I have added the console.log(result), update my question - result is undefined Commented May 8, 2013 at 23:23
  • It's undefined because you declared it again before you logged it. See my answer below. Commented May 8, 2013 at 23:29

3 Answers 3

1

You need to get the returned data by the php and do something with it. See the added line of code below.

jQuery("#ajaxForm").submit(function(event){
event.preventDefault();

    var firstName = jQuery("#firstName").val();
    var age = jQuery("#age").val();

    // jQuery.ajax() - Perform an asynchronous HTTP (Ajax) request.
    jQuery.ajax({
        type: "POST",
        url: "http://localhost/profiling/index.php",
        data: {fname:firstName, id:age}
    }).done(function(result){
        alert("Your data has been submitted!" + firstName);
        alert("This is the data returned by the php script: " + result)
    });

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

4 Comments

Ty! Weird, the result - alerts me and gives me the whole content of the page.
I may not understand the ajax very well, shouldn't the echo $_POST["fname"] work after the ajax submit?
What else is contained in the php script?
Yep, so, just write a very short script that returns just fname if that's all you want.
0

You have to use the success callback function to process the response from the POST to your Php page.

As stated in this thread

Your code could look similar to the following:

  /* Send the data using post and put the results in a div */
$.ajax({
  url: "test.php",
  type: "post",
  data: values,
  success: function(returnval){
      alert("success");
       $("#result").html('submitted successfully:' + returnval);
  },
  error:function(){
      alert("failure");
      $("#result").html('there is error while submit');
  }   
}); 

So, you have to somehow append the response from your Php to an HTML element like a DIV using jQuery

Hope this helps you

7 Comments

$_POST["fname"] - I should be able to echo this after the Ajax submit
additionaly, add a datatype. Html or json.
@webmasters Yes, your Php is fine, you are able to echo the $_POST["fname"] variable, but then how does the HTML knows that it has to show that value? That is when jQuery comes to action by displaying the Php echoed value somewhere in your page. Does that makes any sense?
I see, that makes sense, cause I'm not refreshing the page... but how do I do that?
@juanreyesv that script doesn't do anything with the returned data.
|
0

The correct way:

<?php
$change = array('key1' => $var1, 'key2' => $var2, 'key3' => $var3);
echo json_encode(change);
?>

Then the jquery script:

<script>
$.get("location.php", function(data){
var duce = jQuery.parseJSON(data);
var art1 = duce.key1;
var art2 = duce.key2;
var art3 = duce.key3;
});
</script>

Comments

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.