0

I am making an AJAX call to a php file on my local domain. I am running into an issue as it is not returning any data for the success function in the request.

I am serializing a form to post data to go into a DB. I can get this working just fine and it is writing to the database.

The issue is when I try to get the echoed statement to show in the data part of the success function.

I have used alert and it doesn't alert anything. I have tried a console.log and that just shows:

(an empty data string)

I had it working before I switched from mysql_ functions to PDO functions. Now it just isn't returning anything. I have had a look around and this link in particular and it still isn't working.

This is what I have in the ajax post request:

var formstuff = $('#person-data').serialize();
$.ajax(
{
    url: 'insert.php',
    type: "POST",
    dataType: 'text',
    data: formstuff,
    success: 
    function(data)
    {
        console.log(data);
        alert(data);
        $('#info').hide();
        $('#info').html('');
        $('#info').append(data);
        $('#info').fadeIn(1500);
        $("#person-data")[0].reset();
        $('#username').css({'border': '1px solid #000', 'background-color': '#FFF', 'color': '#000'});
        $('#username').css('background-image', 'url(images/txtbox/id.png)');
    }
});

I have checked the posted headers in the console and it is posting what is entered into the form fields.

This is the echo that I have that I want returned to the success function:

echo 'ID: '.$idp.' Lastname: '.$ln.' First Name: '.$fn.' added to the database';

This is just not being returned to the success function. I have tried this without using the try...catch. I have put it inside the try and nothing. The catch error is being sent back to the function as it should be.

I have tried to echo something before the try and then outside of the catch and the echo before the try is outputted to the success function, just not the data I need.

I have also tried if statements to see if that will work and it isn't displaying anything.

I am stumped and just wondered if there were some pointers I could get.

TIA

Edit

PDO info:

try
{
    $sqlp = $conn->prepare("INSERT INTO ".PERSON." (lastname, firstname, dob, adbkid) VALUES(:ln, :fn, :dob, :unp)");
    $sqla = $conn->prepare("INSERT INTO ".ADDRESS." (address1, address2, town, county, postcode, country, lat, lng, personID) VALUES (:a1, :a2, :town, :county, :pc, :country, :lat, :lng, :una)");
    $sqlc = $conn->prepare("INSERT INTO ".CONTACT." (landline, mobile, email1, email2, personID) VALUES (:lp, :mp, :e1, :e2, :unc)");
    $sqlm = $conn->prepare("INSERT INTO ".MISC." (web, notes, photo, fbid, twitter, cat, personID) VALUES (:web, :notes, :pic, :fb, :tw, :cat, :unm)");
    $sqlv = $conn->prepare("INSERT INTO ".VARI." (Time, Date, ipaddress, browser, os, personID) VALUES (:it, :idate, :iip, :ib, :ios, :unv)");

    $sqlp->bindValue(':ln', $ln);
    $sqlp->bindValue(':fn', $fn);
    $sqlp->bindValue(':dob', $dob);
    $sqlp->bindValue(':unp', $un);

    $sqla->bindValue(':a1', $a1);
    $sqla->bindValue(':a2', $a2);
    $sqla->bindValue(':town', $town);
    $sqla->bindValue(':county', $county);
    $sqla->bindValue(':pc', $pc);
    $sqla->bindValue(':country', $country);
    $sqla->bindValue(':lat', $lat);
    $sqla->bindValue(':lng', $lng);
    $sqla->bindValue(':una', $un);

    $sqlc->bindValue(':lp', $lp);
    $sqlc->bindValue(':mp', $mp);
    $sqlc->bindValue(':e1', $e1);
    $sqlc->bindValue(':e2', $e2);
    $sqlc->bindValue(':unc', $un);

    $sqlm->bindValue(':web', $web);
    $sqlm->bindValue(':notes', $notes);
    $sqlm->bindValue(':pic', $pic);
    $sqlm->bindValue(':fb', $fb);
    $sqlm->bindValue(':tw', $tw);
    $sqlm->bindValue(':cat', $cat);
    $sqlm->bindValue(':unm', $un);

    $sqlv->bindValue(':it', $it);
    $sqlv->bindValue(':idate', $idate);
    $sqlv->bindValue(':iip', $iip);
    $sqlv->bindValue(':ib', $ib);
    $sqlv->bindValue(':ios', $ios);
    $sqlv->bindValue(':unv', $un);

    $sqlp->execute();
    $sqla->execute();
    $sqlc->execute();
    $sqlm->execute();
    $sqlv->execute();

    $idp = $sqlp->lastInsertId();
    $ida = $sqla->lastInsertId();
    $idc = $sqlc->lastInsertId();
    $idm = $sqlm->lastInsertId();
    $idv = $sqlv->lastInsertId();

    echo $idp.' '.$ln.' '.$fn.' added';
}
catch (PDOException $e)
{
    print '<div class="sqlerror">'.$e->getMessage().'</div>';
}

This is what is returned and the response is shown in the file using mysql

Response from mysql

This is what is returned when using PDO - no data and no response Response from PDO

Community
  • 1
  • 1
Chris
  • 431
  • 1
  • 5
  • 16
  • If your problem occurred while migrating from `mysql_*` to PDO, can you share some of your PDO code? – scrowler Jul 13 '14 at 22:26
  • 1
    have you tried looking in the network tab of your browser's dev tools to see if anything is coming back? If you changed something one the server side and now it's not working, I'd start there. – MrOBrian Jul 13 '14 at 22:29
  • I was looking at the console to see what was coming back and all it had was the name of the php file with a 200 success - this is why I did console.log() and that showed the empty data string message. The only thing that has changed is switching from mysql to PDO. I have just tried with PDO and then mysql and I get a response from the mysql one and not the PDO one. I'll add a screenshot to the original question – Chris Jul 13 '14 at 22:34
  • What does respond the insert.php on response tab from firebug with PDO? –  Jul 13 '14 at 22:48
  • 1
    Try logging the results of your query on the server... verify that you are getting the results there before worrying about the Ajax response (which might be hiding the real issue) – scunliffe Jul 13 '14 at 22:49
  • @CarlierRobin nothing responds unless it is a caught exception error. It doesn't even show a response tab under insert.php – Chris Jul 13 '14 at 22:53
  • @scunliffe After every submit of the form I am checking the database and the records are there as they should be. Not sure how to log it to the server. I would assume you mean an error log? It isn't showing anything in an error log – Chris Jul 13 '14 at 22:54
  • I mean you are intending to return some data... log that data to make sure it isn't null/empty/etc. .. – scunliffe Jul 13 '14 at 23:02
  • I am indeed intending it to return a message to say that it was inserted into the database. Not quite sure how to log to the server. As I say, console.log() shows it as empty so would assume the same would happen server side too – Chris Jul 13 '14 at 23:25

1 Answers1

1

Function lastInsertId() is a method that apply on a PDO object, you try here to call it on a PDOStatement object, try this :

$sqlp->execute();
$idp = $conn->lastInsertId();
$sqla->execute();
$ida = $conn->lastInsertId();
$sqlc->execute();
$idc = $conn->lastInsertId();
$sqlm->execute();
$idm = $conn->lastInsertId();
$sqlv->execute();
$idv = $conn->lastInsertId();

instead of

$sqlp->execute();
$sqla->execute();
$sqlc->execute();
$sqlm->execute();
$sqlv->execute();

$idp = $sqlp->lastInsertId();
$ida = $sqla->lastInsertId();
$idc = $sqlc->lastInsertId();
$idm = $sqlm->lastInsertId();
$idv = $sqlv->lastInsertId();
  • Thank you, thank you, thank you. That did it. It was the lastInsertId() function that was not allowing the data to be returned. Been on this a couple of days before I posted here. :D – Chris Jul 13 '14 at 23:28