在下面的代码中我将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!");
}
}
});
});
在这段代码中,我想在有一些行时指示成功,否则它应该指示失败。
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 "\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...";
}
答案 0 :(得分:0)
在JavaScript代码中,您提供了ipdNo
作为AJAX参数,但在PHP文件中,您尝试通过ipd_no
访问名为$_POST
的未定义键。我还建议将AJAX dataType
从"html"
更改为"text"
,因为您只是在PHP文件中回显一些纯文本。
在PHP文件中,为了使用PDO::commit
或PDO::rollBack
,您需要先调用PDO::beginTransaction
。
在致电PDOStatement::fetch
之前,您需要通过PDOStatement::execute
执行您的陈述。
在if
语句中,您需要将$row >==0
的语法错误语句更改为$row!==false && count($row)>0
之类的语句。最后,请注意您上次if
语句中没有任何匹配的else
语句,其中您评论了//if ends here..
,但是;它可能在您的代码段中不可见。
此外,最好始终检查来自任何方法调用或函数调用的返回结果。