在下面的代码中,我将ipdno作为param传递,然后从服务器获得响应,因为这是我的代码。
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...";
}发布于 2015-03-10 13:44:26
在JavaScript代码中,您提供了ipdNo作为AJAX参数,但是在PHP文件中,您尝试通过$_POST访问名为ipd_no的未定义密钥。我还建议将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..;在您的代码片段中可能看不到它。
此外,最好始终检查任何方法调用或函数调用的返回结果。
https://stackoverflow.com/questions/28960149
复制相似问题