【问题标题】:How to get the AJAX result in PHP?如何在 PHP 中获取 AJAX 结果?
【发布时间】:2016-02-23 07:46:54
【问题描述】:

当结果存在时,它不会转到if 条件,而是转到else。在查询结果$num>0 我想要一个类似“收据号已经存在”的警报。

$.post("acceptajax.php?confirm1="+confirm+'&receipt='+receipt+'&amount='+amount,function(result,status){
    //$('#divsub'+confirm).html(data);
    if(result == 'exist'){
        alert('Receipt number Alerady Exit');
    } else {
        $('#accept_'+confirm).hide();
        $('#unaccept_' +confirm).show();
    }
});

acceptajax.php:

$img_id=$_GET['confirm1'];
$receipt=$_GET['receipt'];
$receipt='HYD'.$receipt;
$amount=$_GET['amount'];
$sql=mysql_query("SELECT receipt FROM user WHERE receipt=$receipt");
$num=mysql_num_rows($sql);
if($num==0){
    echo "suc";
} else {
    echo "exist";
}

【问题讨论】:

    标签: php jquery mysql ajax


    【解决方案1】:

    您在$receipt 附近缺少'。试试这个:

    "SELECT receipt FROM user WHERE receipt='$receipt'"
    

    旁注:

    1) 请不要使用mysql_。它已被弃用并删除 从 PHP 7 开始。使用 mysqli_*PDO

    2) 您的查询已为 sql 注入打开。读这个 : How can I prevent SQL injection in PHP?

    【讨论】:

      【解决方案2】:

      使用这样的查询:

      $sql=mysql_query("SELECT receipt FROM user WHERE receipt='".$receipt."'");
      

      而不是使用它,因为这不是正确的编码方式,因为您使用 post 方法发送数据并使用 get 方法发送数据。

      $.post("acceptajax.php?confirm1="+confirm+'&receipt='+receipt+'&amount='+amount,function(result,status)
      

      你应该这样使用它:

      $.post("acceptajax.php", {confirm1:confirm, receipt: receipt, amount: amount}, function(result, status){
         //your script
       });
      

      并使用 $_POST[] 获取 acceptajax.php 上的数据;方法。希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-10
        • 2021-11-17
        • 2012-04-10
        相关资源
        最近更新 更多