请帮帮我。提前致谢。实际上我有一个带有动态行的html表,用于循环并在底部提交按钮。在行的开头有一个复选框,所以我怎么能只将特定的复选框行值插入mysql数据库。例如,如果我检查第一行和第三行,那么只有特定的行数据应插入到mysql数据库中。 我当前的代码是将所有表行插入到mysql数据库中,为什么因为我在我的jquery代码中使用了每个函数。所以请帮助我如何在选中复选框并单击“提交”按钮时,只能将所选行值插入到数据库中。在我的代码中,php语法在cakephp中,我只需要传递所选复选框的整行值
My code as below:
<!DOCTYPE html>
<html lang="en">
<head>
<?php echo $this->Html->css('jquery-ui.min.css'); ?>
<?php echo $this->Html->script('jquery.js'); ?>
<?php echo $this->Html->script('jquery-ui.min.js'); ?>
<?php echo $this->Html->script('jquery.validate.min'); ?>
<?php echo $this->Html->css('sb-admin-2'); ?>
<?php echo $this->Html->css('sb-admin-2.min'); ?>
<?php echo $this->Html->script('sb-admin-2'); ?>
<?php echo $this->Html->script('sb-admin-2.min'); ?>
<?php echo $this->Html->css('bootstrap.min');?>
<?php echo $this->Html->script('bootstrap');?>
<?php echo $this->Html->script('bootstrap.min'); ?>
<style type="text/css">
.row_selected {background-color: #BFCBD7;}
</style>
<script>
$('#orderSave').on('click', function(e) {
e.preventDefault();
$('.rowCont tr').each(function(row, tr){
var TableData = new Array();
TableData[row] = {
"userid" : $('#userid').text(),
"user_name" : $('#user').text(),
"date" : $(tr).find('td:eq(3)').text(),
"exam" : $(tr).find('td:eq(4)').text(),
"venue" : $(tr).find('td:eq(5)').text(),
"price" : $(tr).find('td:eq(6)').text(),
"total_orders" : $(tr).find('td:eq(7)').text(),
"amount" : $(tr).find('td:eq(8)').text(),
//"orders_completion" : $(tr).find('.chk')[0].checked
"orders_completion" : $('#checked').val()
}
TableData = JSON.stringify(TableData);
console.log(TableData);
//alert(TableData);
$.ajax({
type : "POST",
url : "/invl_exams/ordercompletion",
cache : "false",
data : {data:TableData},
success : function(result){
console.log(result);
}
});
});
});
});
</script>
</head>
<body>
<p> </p>
<?php $uid = $this->Session->read('Auth.User.id'); ?>
<p style="font-size:25px;text-align:left;padding: 0px 0px 0px 275px">User's Order List</p>
<div class="container">
<div class="row">
<div class="col-md-10">
<div><b>Name : </b><?php echo $usersadmins[0]['carts']['username']; ?>
<input type="hidden" name="hidename" value="<?php echo $usersadmins[0]['carts']['username']; ?>">
</div>
<div> </div>
<div>
<form role="form" accept-charset="utf-8" method="post" id="examForm" action="/invl_exams/users/orderCheck">
<table class="table table-bordered rowCont" id="sourcetable">
<tr>
<!--<th>S.No</th>-->
<th>Order Completion</th>
<th>Exam Date</th>
<th>Exam Name</th>
<th>Venue</th>
<th>Price / Course</th>
<th>No of Orders</th>
<th>Amount</th>
</tr>
<?php
$j = 1;
for($i=0;$i<count($usersadmins);$i++)
{
?>
<tr>
<!--<td><?php //echo $j++;?></td>-->
<td>
<input type="checkbox" name="chk[]" id="checked" class="chk" value="1">
</td>
<td style="display:none" id="userid"><?php echo $usersadmins[0]['carts']['userid']; ?></td>
<td style="display:none" id="user"><?php echo $usersadmins[0]['carts']['username'];?></td>
<td id="date"><?php echo $usersadmins[$i]['carts']['date']; ?></td>
<td id="exam"><?php echo $usersadmins[$i]['carts']['exam_name']; ?></td>
<td id="venue"><?php echo $usersadmins[$i]['carts']['venue']; ?></td>
<td id="price"><?php echo "$".$usersadmins[$i]['carts']['price']; ?></td>
<td id="torders"><?php echo $usersadmins[$i]['carts']['noOf_orders']; ?></td>
<td id="amount"><?php echo "$".$usersadmins[$i]['carts']['amount']; ?></td>
</tr>
<?php } ?>
<tr>
<td colspan="6"> </td>
<td><button type="button" class="btn btn-success btn-xs" id="orderSave">Submit</button></td>
</tr>
</table>
</div>
</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
试试这个:
<script>
$('#orderSave').on('click', function(e) {
e.preventDefault();
$('.rowCont tr').each(function(row, tr){
if($(this).find('input').is(':checked')) {
var TableData = new Array();
TableData[row] = {
"userid" : $('#userid').text(),
"user_name" : $('#user').text(),
"date" : $(tr).find('td:eq(3)').text(),
"exam" : $(tr).find('td:eq(4)').text(),
"venue" : $(tr).find('td:eq(5)').text(),
"price" : $(tr).find('td:eq(6)').text(),
"total_orders" : $(tr).find('td:eq(7)').text(),
"amount" : $(tr).find('td:eq(8)').text(),
//"orders_completion" : $(tr).find('.chk')[0].checked
"orders_completion" : $('#checked').val()
}
TableData = JSON.stringify(TableData);
console.log(TableData);
//alert(TableData);
$.ajax({
type : "POST",
url : "/invl_exams/ordercompletion",
cache : "false",
data : {data:TableData},
success : function(result){
console.log(result);
}
});
}
});
});
});
</script>