Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ajax调用在php中无法正常工作_Php_Ajax - Fatal编程技术网

ajax调用在php中无法正常工作

ajax调用在php中无法正常工作,php,ajax,Php,Ajax,在下面的代码中,我将ipdno作为一个参数传递,然后我从服务器得到响应,因为这是我的代码 php $('#print').click(function(){ var ipdNo = $('#hid_ipd_id').val(); var param = "ipdNo="+ipdNo; alert("Param: "+param); $.ajax({ u

在下面的代码中,我将
ipdno
作为一个参数传递,然后我从服务器得到响应,因为这是我的代码

php

$('#print').click(function(){
            var ipdNo = $('#hid_ipd_id').val();         
            var param = "ipdNo="+ipdNo;
            alert("Param: "+param);
            $.ajax({
                url: "ipd_bill_print.php", //The url where the server req would we made.
                async: true,
                type: "POST", //The type which you want to use: GET/POST
                data: param, //The variables which are going.
                dataType: "html",
                success: function(data){
                    //alert("Result: "+data+"\nRefreshing page... ");   
                    if(data=='success'){
                        alert("Record updated succcessfully!");
                        location.reload(true);
                    }else{
                        alert("Record could not be updated!");
                    }
                }
            });

        });
<?php
    require_once("db/include.php");

    $ipd_no = $_POST['ipd_no'];
    $token = "Empty";

    try{
        $dbh = getConnection();
        $flag = true;


        $sql = "SELECT ipd_reg_no 
                    FROM  ipd_bill 
                    WHERE ipd_reg_no = ?";
        $sth = $dbh->prepare($sql);
        $sth->bindParam(1,$ipd_no);
        $row = $sth->fetch(PDO::FETCH_ASSOC);
        echo $row;
        if($row >==0)
            $flag = false;
        if($flag)
            echo "success";
        else{
            $dbh->rollback();
            echo "fail";
        }
        //echo "\n FLAG: $flag \n";
        $dbh->commit(); 

    }catch(PDOException $e){
        print($e);
        try{
            $dbh->rollback();
        }catch(PDOException $e){
            die($e->getMessage());  
        }
    }
    else{   //if ends here..
        echo "Outside if...";
}
在这段代码中,我希望在有一些行时指示成功,否则它应该指示失败

ipd\u bill\u print.php

$('#print').click(function(){
            var ipdNo = $('#hid_ipd_id').val();         
            var param = "ipdNo="+ipdNo;
            alert("Param: "+param);
            $.ajax({
                url: "ipd_bill_print.php", //The url where the server req would we made.
                async: true,
                type: "POST", //The type which you want to use: GET/POST
                data: param, //The variables which are going.
                dataType: "html",
                success: function(data){
                    //alert("Result: "+data+"\nRefreshing page... ");   
                    if(data=='success'){
                        alert("Record updated succcessfully!");
                        location.reload(true);
                    }else{
                        alert("Record could not be updated!");
                    }
                }
            });

        });
<?php
    require_once("db/include.php");

    $ipd_no = $_POST['ipd_no'];
    $token = "Empty";

    try{
        $dbh = getConnection();
        $flag = true;


        $sql = "SELECT ipd_reg_no 
                    FROM  ipd_bill 
                    WHERE ipd_reg_no = ?";
        $sth = $dbh->prepare($sql);
        $sth->bindParam(1,$ipd_no);
        $row = $sth->fetch(PDO::FETCH_ASSOC);
        echo $row;
        if($row >==0)
            $flag = false;
        if($flag)
            echo "success";
        else{
            $dbh->rollback();
            echo "fail";
        }
        //echo "\n FLAG: $flag \n";
        $dbh->commit(); 

    }catch(PDOException $e){
        print($e);
        try{
            $dbh->rollback();
        }catch(PDOException $e){
            die($e->getMessage());  
        }
    }
    else{   //if ends here..
        echo "Outside if...";
}

在JavaScript代码中,您提供了
ipdNo
作为AJAX参数,但在PHP文件中,您试图通过
$\u POST
访问名为
ipd\u no
的未定义键。我还建议将AJAX
的“html”
更改为
的“text”
,因为您只是在回显PHP文件中的一些纯文本

在PHP文件中,要使用
,需要首先调用

在调用
之前,您需要通过
执行语句

if
语句中,您需要将语法错误的
$row>==0
语句更改为类似
$row==假计数($row)>0
。最后,考虑到你没有任何匹配的<代码>如果 >语句为你上次<代码> E/<代码>语句,你在这里评论“代码> / /如果在这里结束…< /代码>,但是;它可能在您的代码片段中不可见


此外,您最好始终检查任何方法调用或函数调用的返回结果。

我说,请阅读有关AJAX调用的更多信息,如果您编写数据类型:“JSON”,您可以将所有php参数作为JSON返回。您还应该了解如何使用json_encode()以及您的问题是什么?问题是什么?你的问题是什么?假设在我的表格中没有我想要的ipd_reg_不exit@Fazovsky成功还显示记录无法更新!消息