AJAX函数没有得到PHP的正确响应?

时间:2017-05-26 17:01:12

标签: javascript php jquery ajax

我编写了一个JQuery和PHP脚本, 从PHP成功返回后,AJAX函数应该捕获成功响应,但我没有得到它。 以下是代码:

$.ajax({

                    url     :"script_admin-add-category.php",
                    method  :"POST",
                    data    :{lExpensesId:lcl_ExpensesId},

                    success:function(data){
                        //if(data=="ok"){
                            if(data=="YES"){
                                alert("EMAIL");
                            }else{
                                alert(data);
                            }
                        //}
                        //if(data=="ok"){
                            //alert("Expenses Id already exists!");
                        //}else{
                            //alert(data);
                        //}
                    }

        });

这里是php代码

//Check connection
    if(!$conn){
        die("Connection Failed: " .mysqli_connect_error());
    }else{
//echo "helloooo";
            if(isset($_POST["lExpensesId"])){
                $lExpensesId    = $_POST["lExpensesId"];
                    $Lquery = "SELECT ExpensesId FROM tblexpensestype WHERE ExpensesId = '$lExpensesId'";
                    if($query_result = mysqli_query($conn, $Lquery)){

                            if(mysqli_num_rows($query_result )){
                                echo 'YES';
                            }else{
                                //echo "Proceed";
                            }
                    }else{
                        echo "Not Okay";
                    }
            }else{

            }
    }       

我也可以在浏览器和警报值上看到回显值。但是,如果条件不成功函数???

4 个答案:

答案 0 :(得分:0)

尝试为返回的数据设置正确的数据类型。

$.ajax({
  url: 'script_admin-add-category.php',
  method: 'POST',
  data: {lExpensesId: lcl_ExpensesId},
  dataType: 'text',
  success: function (data) {
    if (data === 'YES') {
      alert('EMAIL')
    } else {
      alert(data)
    }
  }
})

答案 1 :(得分:0)

@J Salaria我理解你的问题,因为你没有得到你想要的结果,你有jquery AJAXPHP代码的问题。有不同的方法通过jquery ajax发送数据,我将详细解释。

$_POST["lExpensesId"]你从HTML <form>获得此ID吗?因为在这里我将向您展示3种不同的实践方式通过ajax发送数据..

  

注意:您的代码对SQL INJECION是易受攻击的。我也将向您展示超越的方法。如果您想了解更多有关SQL注入的信息,请点击此链接SQL INJECTION LINK

HTML表格代码:

<form action="" id="send_lExpensesId_form" method="post">
    <input type="text" name="lExpensesId" id="lExpensesId" >
    <input type="submit" name="submit" >
</form>

发送数据的第一种方式HTML <FORM>

 <script>
    $(document).ready(function(){
        $("#send_lExpensesId_form").submit(function(e){
            e.preventDefault();

            var form_serialize = $(this).serialize();

            $.ajax({
                type:'POST',
                url:'script_admin-add-category.php',
                data:form_serialize,
                success:function(data){
                    if(data == "YES"){
                        alert("EMAIL");
                    }else{
                        alert(data);
                    }
                }
            });
        });
    });
</script>

发送数据的第二种方式HTML <FORM>

    <script>
    $(document).ready(function(){
        $("#send_lExpensesId_form").submit(function(e){
            e.preventDefault();

            var form_serialize = new FormData($(this)[0]);


            $.ajax({
                type:'POST',
                url:'script_admin-add-category.php',
                data:form_serialize,
                contentType: false,
                processData: false,
                success:function(data){
                    if(data == "YES"){
                        alert("EMAIL");
                    }else{
                        alert(data);
                    }
                }
            }); 
        });
    });
</script>

发送数据的第三种方式使用WHEN A LINK CLICKED OR TO DELETED THROUGH ID OR CLASS

 <script>
    $(document).ready(function(){
        $("#send_lExpensesId_form").submit(function(e){
            e.preventDefault();

            var lcl_ExpensesId = $("#lExpensesId").val();

            $.ajax({
                type:'POST',
                url:'script_admin-add-category.php',
                data:{lExpensesId:lcl_ExpensesId},
                success:function(data){
                    if(data == "YES"){
                        alert("EMAIL");
                    }else{
                        alert(data);
                    }
                }
            });
        });
    });
</script>

这是带有mysqli_real_escape_string(); SQL注入的PHP代码

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "admin";
    $dbname = "demo";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    if(isset($_POST["lExpensesId"])){
        $lExpensesId = mysqli_real_escape_string($conn, $_POST["lExpensesId"]);

        $Lquery = "SELECT ExpensesId FROM tblexpensestype WHERE ExpensesId = '$lExpensesId'";
        if($query_result = mysqli_query($conn, $Lquery)){

            if(mysqli_num_rows($query_result )){
                echo 'YES';
            }else{
                echo "Proceed";
            }
        }else{
            echo "Error".mysqli_connect_error();
        }
    }
?>

其他PHP代码MYSQLI->PREPARED对SQL注入更好

<?php
    // WITH MYSQLI PREPARED STATEMENT AGAINST SQL INJECTION
    $sql = $conn->stmt_init();
        $Lquery = "SELECT ExpensesId FROM tblexpensestype WHERE ExpensesId =?";
        if($sql->prepare($Lquery)){
            $sql->bind_param('i',$lExpensesId);
            $sql->execute();
            $sql->store_result();

            if($sql->num_rows > 0){
                echo 'YES';
            }else{
                echo "Proceed";
            }
        }
        else
        {
            echo "Error".mysqli_connect_error();
        }
?>

如果您有其他疑惑,请向我提出您的问题

答案 2 :(得分:0)

所有方法都是众所周知的,非常感谢您的帮助。我的问题是为什么我无法从PHP获得正确的回报。以下是我的代码:

var lcl_ExpensesId = $("#IExpensesId").val();
            //alert(lcl_ExpensesId);
        $.ajax({

                    url     :"script_admin-add-category.php",
                    method  :"POST",
                    data    :{lExpensesId:lcl_ExpensesId},
                    success:function(data){
                        if(data=="ok"){
                            alert("Inserted");
                        }else{
                            alert(data);
                        }
                    }
        });
ob_start();

/ ------------------阅读账户下放费用列表的功能------------------ ----- /

require_once 'db_config.php';
$newlist    = fxn_CONFIGURATION();
$HOST       = $newlist[0];
$DBNAME     = $newlist[1];
$UNAME      = $newlist[2];
$PSWD       = $newlist[3];
$conn   =   mysqli_connect($HOST, $UNAME, $PSWD, $DBNAME);


    //Check connection
    if(!$conn){
        die("Connection Failed: " .mysqli_connect_error());
    }else{
            if(isset($_POST["lExpensesId"])){

                $lExpensesId    = $_POST["lExpensesId"];

                    $Lquery = "SELECT ExpensesId FROM tblexpensestype WHERE ExpensesId = '$lExpensesId'";
                    $query_result = mysqli_query($conn, $Lquery);

                            if(mysqli_num_rows($query_result) > 0){
                                echo "ok";
                            }else{
                                echo "Proceed";
                            }

            }
    }       
mysqli_close($conn);


ob_flush();

因为,我在我的输入密钥方法中使用这个AJAX,所以不管我输入什么,每次都会执行PHP脚本。我在数据库中有一个食物作为食物。当我输入&#34; F&#34;时,我得到了Proceed,&#34; O&#34; - 继续,&#34; O&#34; - 继续,&#34; D&#34; - 好.... 当我输入D时,我应该得到&#34;插入&#34;而不是Ok .... 这是我怀疑为什么我得到这个????

答案 3 :(得分:0)

上面的问题是通过在PHP中使用exit()语句来解决的,因为我在我的值之后得到5 and并且这意味着我有5行html而没有关闭?&gt;。因此,解决问题的最佳方法是根据需要在PHP中使用exit()