I have jQuery Ajax request that sends data to a PHP page that then insert a record into MySQL.
With my current setup, this all works fine.
However, as part of the PHP script, i use mysql_insert_id() to retreive the ID of the last record.
I need the Ajax Success function to return this ID value as a javascript variable.
PHP Code
if ($_GET['Ajax'] == 1) {
$insert_statement = "INSERT INTO ...";
$insert = mysql_query(...);
$LastID = array('LastID' => mysql_insert_id());
$LastID = json_encode($LastID);
}
Javascript
$.ajax({
type: "GET",
url: "AddFunction.php",
data: {"Ajax":"1", "Data":"Test"},
dataType: "json",
success: function(data) {
var LastID = data;
alert(LastID);
}
});
I've read of using PHP's json_encode to create a JSON object and then pass that back, but i've had no luck.
How can i return this value?