dqdjfb2325 2014-07-13 22:24
浏览 88
已采纳

AJAX请求返回空数据集

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

  • 写回答

1条回答 默认 最新

  • dsfsdf7852 2014-07-13 23:04
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型