php - Ajax不发送数据php
问题描述
我有一个问题似乎已经持续了大约一天半;它让我彻夜难眠。我有一个如下所示的表格,我想单击一个按钮,然后相应地更新并将数据插入数据库。
上图为表格,点击支付后显示下图
为 Pinnacle 输入 8600 应该会使 pinnacle 的总汇款增加8,600,但之后没有任何反应。我成功了,但没有更新或插入数据库的表中。
上图显示values提交成功。创建了一个 ajax 来支持这一点,如下所示
<script>
function payCompany(id, companyName, owed, oldRemit){
swal("Enter Amount:", {
title: companyName,
buttons: true,
closeModal: true,
content: "input",
})
.then((amount) => {
if (amount === "") {
swal("Oops!! You need to enter value.");
return false
}else{
$.ajax({
type: 'POST',
url: 'remit.php',
data:{
rid: id,
pay: amount,
company_Name: companyName,
old_Remit: oldRemit,
debt_owed: owed,
<?php
echo ' currentDate : "'.$savedate.'",'
?>
},
success: function(data){
swal(`Paid: ${amount} to ${companyName}`);
},
error: function(data){
swal(`Error remitting: ${amount} to ${companyName}`);
}
});
}
});
};
</script>
应该发送数据到
<?php
#remit.php
/*
If you are using sessions you need to start a session!
*/
error_reporting( E_ALL );
session_start();
if( empty( $_SESSION['useremail'] ) OR empty( $_SESSION['role'] ) OR $_SESSION['role']=="Admin" ){
exit( header('Location: index.php') );
}
/*
Check that all fields that are required in the sql have been submitted
*/
if( isset(
$_POST['rid'],
$_POST['pay'],
$_POST['currentDate'],
$_POST['compnyName'],
$_POST['old_remit'],
$_POST['debt_owed']
) ){
try{
include_once 'connectdb.php';
/*
When inserting, updating multiple tables there is some sense in using a transaction
so that if one part fails the db is not littered with orphan records
*/
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$pdo->beginTransaction();
$remitID = $_POST['rid'];
$new_remit = (double)$_POST['pay'];
$company = $_POST['compny_Name'];
$old_rem = (double)$_POST['old_remit'];
$total_debt = (double)$_POST['debt_owed'];
$current_date = $_POST['currentDate'];
$paid = $new_remit + $old_rem;
$currentDebt = $total_debt - $paid;
/*
The sql command should use placeholders rather than embedded variables - the names are arbitrary
*/
$sql='INSERT INTO `tbl_category_remits` ( `payment_date`, `category`, `remitted` )
VALUES
( :pay_date, :comp, :remit )';
$stmt=$pdo->prepare( $sql );
$args=array(
':pay_date' => $current_date,
':comp' => $company,
':remit' => $new_remit
);
if( !$stmt->execute( $args ) )echo 'stmt#1 failed';
$sql='UPDATE `tbl_category` SET `remitted` =:payment , `debt` =:current_debt WHERE `catid`=:rid';
$stmt=$pdo->prepare( $sql );
$args=array(
':current_debt' => $currentDebt,
':payment' => $paid,
':rid' => $remitID
);
if( !$stmt->execute( $args ) )echo 'stmt#2 failed';
/*
If it all went well, commit these statements to the db
*/
if( !$pdo->commit() )echo 'commit failed';
}catch( PDOException $e ){
/*
Any problems, rollback the transaction and report issues -
not necessarily with the full `getMessage()` ~ perhaps just
'Error!' etc
*/
$pdo->rollBack();
echo $e->getMessage();
}
}
?>
这是我的代码出现的主要延迟。我会很感激你的帮助。谢谢。
解决方案
推荐阅读
- mongodb - 无法从 MongoDB 解码嵌入式文档(嵌套)
- r - 根据值和出现过滤相关矩阵
- javascript - 使用 Elvis (?) 运算符后,如何使 VSCode 为 .JS 文件保持正确的语法突出显示?
- java - 使用 apache qpid-client 连接到 Azure 服务总线的问题
- javascript - 我可以从 test.each 标记模板文字中为另一个上下文捕获测试吗?
- ajax - xhr 参数在哪里?
- c++ - C ++中的'this'是指向引用的指针?
- math - 将 SVG 矩阵变换值转换为 x,y 坐标
- javascript - CSS 过渡不适用于 Javascript
- sql-server - 用于链接 SAP-B1-HANA 服务器的 SSMS 中的简单 SQL 返回空白行