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

in the below code I am passing ipdno as a param, and then I am getting the response from server for that this is my code.

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+"
Refreshing page... ");   
                    if(data=='success'){
                        alert("Record updated succcessfully!");
                        location.reload(true);
                    }else{
                        alert("Record could not be updated!");
                    }
                }
            });

        });

In this code I want to indicate the success when there are some rows, otherwise it should indicate the failure.

ipd_bill_print.php

<?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 "
 FLAG: $flag 
";
        $dbh->commit(); 

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

In JavaScript code you have provided the ipdNo as the AJAX parameter, but in the PHP file you try to access an undefined key named ipd_no through $_POST. I also recommend to change the AJAX dataType from "html" to the "text", because you are just echoing some plain text in the PHP file.

In the PHP file, in order to use the PDO::commit or PDO::rollBack, you need to first invoke PDO::beginTransaction.

Before calling PDOStatement::fetch, you need to execute your statement through PDOStatement::execute.

In the if statement you need to change the syntactically wrong statement of $row >==0 to something like $row!==false && count($row)>0. Finally, consider that you don't have any matching if statement for you last else statement, where you commented //if ends here.., However; It just maybe not visible in you code snippet.

In addition, you are better to always check the returning result from any method call or function invoke.