sanjihan
sanjihan

Reputation: 6026

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':'[email protected]'},
       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");
}

Upvotes: 0

Views: 1158

Answers (3)

Mamdouh Saeed
Mamdouh Saeed

Reputation: 2324

Just handle your db queries, errors and status using echo or die() and be ware to handle final outputs sent to ajax.

echo "Data inserted";
// or 
die("faild");

From Ajax

success: function(response){console.log(response)};

Upvotes: 0

DsRaj
DsRaj

Reputation: 2328

Usee echo and exit in insertToDB.php

and then

console.log(response);

Upvotes: 2

Tomas Aschan
Tomas Aschan

Reputation: 60664

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.)

Upvotes: 2

Related Questions