With an AJAX call I insert a record in a MySQL database.
MAIN.PHP
-------------
$.ajax({
url:'pages/insert.php',
method:'POST',
data:{
kind:kind,
title:title,
},
error: function() {
alert('Bericht toevoegen is fout gegaan!');
},
success:function(data){
alert(data.id);
}
});
In insert.php the content will be inserted in the datebase. In the table there is a autonumber field 'id'. This field would I like to receive back, so I call directly after the insert: mysqli_insert_id($conn) and echo the result.
INSERT.PHP
------------
$actie = mysqli_query($conn, "INSERT INTO tbCont (User, SoortBericht, Teaser) VALUES ('$user', '$kind', '$title');") or die(mysqli_error($conn));
$last_id = mysqli_insert_id($conn);
echo json_encode(['id'=>$last_id]);
In the main file I would use the id, but I don't receive it back. Alert(data.id) shows me 'undefinded'.
Alert(data) shows me {"id":34133}, thats better. But what is the right syntax to use the id? And what when I will give more results back?