Php 如何在ajax post中使用特定的表行数据
我正试图点击我的支付按钮,以便更新对特定公司的支付。下面是我的用户界面 单击支付按钮可显示此信息 我希望显示付款将要支付的公司的名称,并且能够在汇款总额栏中更新付款,以及减少债务 我在这一页上呆了很长时间。以下是我目前的代码: 查询Php 如何在ajax post中使用特定的表行数据,php,ajax,Php,Ajax,我正试图点击我的支付按钮,以便更新对特定公司的支付。下面是我的用户界面 单击支付按钮可显示此信息 我希望显示付款将要支付的公司的名称,并且能够在汇款总额栏中更新付款,以及减少债务 我在这一页上呆了很长时间。以下是我目前的代码: 查询 error_reporting( E_ALL ); try{ $sql='select c.catid, c.`category`, ifnull(sum( s.`stock_in` * s.`stock_price` ),0) as `deb
error_reporting( E_ALL );
try{
$sql='select c.catid, c.`category`,
ifnull(sum( s.`stock_in` * s.`stock_price` ),0) as `debt`, c.`remitted`
from `tbl_category` c
left outer join `tbl_stock_in` s on s.`category_name` = c.`category`
group by c.`category` order by catid desc;';
$stmt=$pdo->prepare( $sql );
$stmt->execute();
}catch( PDOException $e ){
echo $e->getMessage();
}
表格代码
<?php
while( $rs=$stmt->fetch(PDO::FETCH_OBJ ) ){
echo'
<tr>
<td>'.$rs->catid.'</td>
<td>'.$rs->category.'</td>
<td>
'.number_format($rs->debt,2).'
</td>
<td>'.number_format($rs->remitted,2).'</td>
<td>
<button id='.$rs->catid.' class="btn btn-info btnremit"><span class="glyphicon glyphicon-eye-open" style="color:#ffffff" data-toggle="tooltip" title="Pay Company"></span></button>
</td>
<td>
<a href="#.php?id='.$rs->catid.' class="btn btn-info" role="button"><span class="glyphicon glyphicon-edit" style="color:#ffffff" data-toggle="tooltip" title="Edit Category"></span></a>
</td>
<td>
<button id='.$rs->catid.' class="btn btn-danger btndelete" ><span class="glyphicon glyphicon-trash" style="color:#ffffff" data-toggle="tooltip" title="Delete Category"></span></button>
</td>
</tr>
';
}
?>
这是我的ajax
<script>
$(document).ready(function() {
$('.btnremit').click(function() {
//var tdh = $(this);
var id = $(this).attr("id");
swal("Enter Amount:", {
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,
<?php
echo ' currentDate : "'.$savedate.'", //working
compnyName : "'#'", //how do i call the company name that is clicked
old_remit : "'#'",//how do i fetch the old remit from the table
'
?>
},
success: function(data){
swal(`Paid: ${amount}`);
},
error: function(data){
swal(`Error remitting: ${amount}`);
}
});
}
});
});
});
</script>
$(文档).ready(函数(){
$('.btnremit')。单击(函数(){
//var tdh=$(本);
var id=$(this.attr(“id”);
swal(“输入金额:”{
按钮:是的,
对,,
内容:“输入”,
})
。然后((金额)=>{
如果(金额=“”){
swal(“哎呀!!你需要输入值。”);
返回错误
}否则{
$.ajax({
键入:“POST”,
url:'remote.php',
数据:{rid:id,pay:amount,
},
成功:功能(数据){
swal(`Paid:${amount}`);
},
错误:函数(数据){
swal(`汇款错误:${amount}`);
}
});
}
});
});
});
汇款代码.php*
<?php
#stock-in.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']
) ){
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'];
$current_date = $_POST['currentDate'];
$company = $_POST['compnyName'];
$old_rem = (double)$_POST['old_remit'];
$paid = $new_remit + $old_rem;
/*
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, :price, :cost, :current_times )';
$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 WHERE `catid`=:rid';
$stmt=$pdo->prepare( $sql );
$args=array(
':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();
}
}
?>
请问,当我点击支付时,如何从表中提取公司名称和旧汇款。我需要你的帮助 您可以将国家名称和旧汇款作为对象从php发送,并通过jquery的obj在正确的元素中使用它们
$(document).ready(function() {
$('.btnremit').click(function() {
//var tdh = $(this);
var id = $(this).attr("id");
})
})
您可以使用这个函数,在这里您可以传递id、名称等
function yourFunction(id,companyName,oldRemit){
swal("Enter Amount:", {
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,
compnyName : companyName,
old_remit : oldRemit,
<?php
echo 'currentDate : "'.$savedate.'"'
?>
},
success: function(data){
swal(`Paid: ${amount}`);
},
error: function(data){
swal(`Error remitting: ${amount}`);
}
});
}
});
}
函数您的函数(id、公司名称、旧汇款){
swal(“输入金额:”{
按钮:是的,
对,,
内容:“输入”,
})
。然后((金额)=>{
如果(金额=“”){
swal(“哎呀!!你需要输入值。”);
返回错误
}否则{
$.ajax({
键入:“POST”,
url:'remote.php',
数据:{
里德:身份证,
支付:金额,
公司名称:公司名称,
旧汇款:旧汇款,
},
成功:功能(数据){
swal(`Paid:${amount}`);
},
错误:函数(数据){
swal(`汇款错误:${amount}`);
}
});
}
});
}
从html表行调用此函数,如下所示
<td>
<button onclick="yourFunction('.$rs->catid.','.$rs->category.','.$rs->remitted.')" id='.$rs->catid.' class="btn btn-info btnremit"><span class="glyphicon glyphicon-eye-open" style="color:#ffffff" data-toggle="tooltip" title="Pay Company"></span></button>
</td>
在模式中,您需要公司名称和旧汇款
?感谢您的快速回复。在模式中,我希望公司名称出现。我还希望通过ajax发布公司名称和旧汇款。显示您的“汇款.php”代码。我已经更新了它,请参阅我的此答案,如果它对您有帮助的话。请问我该怎么做?在php中,您可以使用json_encode函数返回数据,如json_encode(['name'=>'test','remote'=>2])
然后您可以通过jqueryvar obj=jquery.parseJSON('{“name”:“test”,“remote”:2})解析此响应
并为元素(“#countryName”).html(obj.name)
设置obj。如果是输入,您可以在模式中使用(“#countryName”).val(对象名称)
如何回显公司名称
,旧汇款
。您可以将swal标题设置为公司名称@感谢这些努力;我实现了它,但出现了一个错误:Uncaught ReferenceError:Pinnacle未定义。请尝试此操作。它实际上选择了名称和指定的所有数据,但未能使用我输入的值更新数据库。