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成功还显示记录无法更新!消息