猿问

Ajax 调用输出相同的消息

我在下面的 ajax 调用中检查了客户表中是否存在 card_number,但是,在这两种情况下,即使卡号存在,我也会得到相同的响应,即它不存在。所以最后两条消息都将是“不存在”


agent_reload.php


<script type="text/javascript">

    $(document).ready(function()

    {

        $("#card_number").change(function() 

        { 

            var card_num = $("#card_number").val();


            $.ajax({  

                type: "POST",  

                url: 'CheckUserCardNumber.php',  

                data: "card_number="+card_number,  

                success: function(msg){            

                    var Result = $.trim(msg);


                    if(Result === "does_not_exists")

                    { 

                        alert("does not exist");

                    }  

                    else  

                    {  

                        alert("exists");

                    }  

                } 

            }); 


            return false;

        });

    });

</script>

支票卡号.php


<?php

include('dbconnection.php');

if(isset($_POST['card_number']))

{

    $Card_Number = $_POST['card_number'];

    $sql = "select * from customers where card_id='$Card_Number'";

    $Result = $db->query($sql);


    if ($Result->num_rows != 0)

    {

        echo "exists";

    }

    else

    {

        echo "does_not_exists";

    }

}


红颜莎娜
浏览 152回答 2
2回答

眼眸繁星

一个提示一个好的编程习惯是使用驼峰命名和适当的缩进。// <!-- CheckCardNumber.php -->&nbsp;<?phpif (isset($_POST['card_number'])) {&nbsp; include('dbconnection.php');&nbsp; $cardNumber = $_POST['card_number'];&nbsp; $sql&nbsp; &nbsp; &nbsp; &nbsp; = "SELECT * from customers where card_id= '$cardNumber'";&nbsp; $result&nbsp; &nbsp; &nbsp;= $db->query($sql);&nbsp; echo $result->num_rows > 0 ? 'Exists' : 'Does not exist.';}解决方案在您的 ajax 请求中,如果您设置了正确的消息,您可以提醒您收到的任何响应。此外,请确保您随请求发送的值正确无误。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: 'CheckUserCardNumber.php',&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: "card_number="+$(this).val(),&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(msg){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert(msg);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp;此外,请始终使用 PDO/Prepared MySQLI。

凤凰求蛊

看起来您将 var 设置为 card_num 但将 card_number 传递给 ajax:var card_num = $("#card_number").val();...data: "card_number="+card_number,
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答