Im getting an empty success function parameter back (lewa). But when I change 'lewa' to order (order.vnaam) it works. Could you point out what the problem is?
$(document).ready(function() {
$.ajax({
type: 'POST',
url: 'json-users.php',
dataType: 'json',
success: function(naam) {
$.each(naam, function(i, item) {
$('ul').append('<li>Voornaam: ' + item.voornaam + '<br>Achternaam: ' + item.achternaam + '</li>');
});
}
}); //end get data db
}); //end document ready function
function upload() {
var order = {
vnaam: $('.voornaam').val(),
anaam: $('.achternaam').val(),
};
$.ajax({
type: 'POST',
url: 'posttest.php',
/*dataType: 'json',*/
data: order,
success: function( lewa ) {
//When action is a success
$('ul').append('<li>Voornaam: ' + lewa.vnaam + '<br>Achternaam: ' + lewa.anaam + '</li>');
console.log(lewa);
console.log(order);
},
error: function() {
//When action is a failure
alert('error running function');
}
});
}
The following code is posttest.php
$voornaam = mysqli_real_escape_string($connect, $_POST["vnaam"]);
$achternaam = mysqli_real_escape_string($connect, $_POST["anaam"]);
$sql = "INSERT INTO users (voornaam, achternaam) VALUES ('".$voornaam."', '".$achternaam."')";
mysqli_query($connect, $sql);
The following code is json-users.php
$array_user = array();
while ( $data = mysqli_fetch_assoc($result) ) {
$array_user[] = $data;
}
echo json_encode($array_user);