sanjihan sanjihan - 26 days ago 11
Ajax Question

getting the server response caused by ajax POST method

I am trying to send POST data to the server. This is the ajax code that I use. On the other side I use mySQL and PHP. How can I see what the

insertToDB.php
output was? Currently my code is not working and I am not sure why. Getting the output from
insertToDB.php
is probably the best way to start.

$.ajax({
url: "insertToDB.php",
type: 'POST',
data: {'lastname':'Cena','name':'John', 'email':'jc@gmail.com'},
success: function(response) {
console.log("success");
}
});


on the PHP side I have:

require_once 'login.php';

$conn = new mysqli($hn,$un,$pw,$db);
if ($conn->connect_error){
die($conn->connect_error);
}


$url = $_POST["lastname"];
$name = $_POST["name"];
$email = $_POST["email"];


$query = "INSERT INTO testtable (url ,name ,email) VALUES ('$url', '$name', '$email')";
$result = $conn->query($query);
if (!$result){
die ("QUERY FAILED");
}

Answer

The first argument to the success function is the output.

That is, if you console.log(response) instead of console.log("success"), you'll see whatever the server wrote back to the response stream.

(If you're talking about log output on the server side, that's an entirely different matter. Then we'll need to see your serverside code, not the AJAX call.)

Comments