dppcyt6157 2015-03-10 09:32
浏览 31

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...";
}
  • 写回答

1条回答 默认 最新

  • doumaji6215 2015-03-10 13:44
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题