0

JQUERY

$.ajax({
        url: 'save.php',
        type: 'POST',   
        success:function () 


        var id = html.$id;
        var name = html.$name;  
        alert(id);
        alert(name); 
    }); 

SAVE.PHP

<?php
    include("db.php");

    $sql = "INSERT into test(Name)values('') ";

    if (mysql_query($sql))
    {
        $id = mysql_insert_id();
        echo $name = "Peter";
        echo $id;
    }
?>

How can retrieve specific data and store in a specific variable in my Jquery? Please ignore my Name in php, because I just randomly assign variable to it

How to return two different data, and store them in 2 variable?

I know it is incorrect, please tell me the correct way of doing it

1
  • read up about jquery ajax calls. Also, read up about parsing objects. You also need to return an object from your php code. Commented Jun 4, 2013 at 2:16

2 Answers 2

1

JS

$.ajax({
    url : 'save.php',
    type: 'POST',
    dataType: 'JSON'
}).done(function(data) {
    alert(data.id);
    alert(data.name); 
});

PHP

<?php
    include("db.php");

    $sql = "INSERT into test(Name)values('') ";

    if (mysql_query($sql)) {
        $arr = array(
                     "id"   => mysql_insert_id(),
                     "name" => "Peter"
                    );

        echo json_encode($arr);
    }
?>
Sign up to request clarification or add additional context in comments.

2 Comments

I could get the return value of the data.id
$.ajax({ url: 'save.php', type: 'POST', dataType: 'JSON', success:function (data) { alert(data.id); } });
0

You can return the value as in JSON format in key-value pair. Since I do not have much knowledge over php, but if possible try to return any JSON or XML response from your php code:

$.ajax({
      url: 'save.php',
      type: 'POST',
      success:function(response) 
        // here you can use response to display your value
      }); 

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.