【发布时间】:2014-10-23 04:48:31
【问题描述】:
我在删除数据中选中的项目时遇到问题,因为我的删除按钮不是我从数据库中获取数据的 for 循环的一部分,这就是为什么我很难获取数据的 ID将被删除。我可以用 javascript modaldelete 进行一次删除,但我不能用复选框实现同样的事情,因为删除按钮不在表格内,如果你把删除按钮放在旁边复选框,因为它对表中的每个项目都有一个 for 循环。现在我不知道如何从选定的复选框中获取值。解决这个问题的更合适的方法是什么? PHP 还是 JavaScript?两种语言哪个更高效?
这是我的代码 sn-p:
<div style="font-size:16px;">
Search: <input id="filter" type="text" style="height:1em;"/>
<p class="pull-right">
<a href="javascript:void(0)" class="modaladdclient" data-id="add">Add</a>
<a href="javascript:void(0)" class="modaldelete" data-id="add">Delete</a>
</p>
</div>
<?php
$statement="SELECT * FROM clients";
$result = mysqlparser($statement, $db);
?>
<table data-filter="#filter" class="footable" style="background-color:white;">
<thead>
<tr>
<th>Client ID</th>
<th>First Name</th>
<th data-sort-initial="ascending">Last Name</th>
<th>Mobile Number</th>
<th>Branch Code</th>
<th data-sort-ignore="true" colspan="2">Options</th>
</tr>
</thead>
<tbody>
<?php for($i=0; $i<count($result); $i++){?>
<tr>
<td><?php echo $result[$i]['Client_ID'];?></a></td>
<td><?php echo $result[$i]['First_Name'];?></td>
<td><?php echo $result[$i]['Last_Name'];?></td>
<td><?php echo $result[$i]['Mobile_Number'];?></td>
<td><a href="javascript:void(0)" class="modalbid" data-id="<?php echo $result[$i]['Branch_Code'];?>"><?php echo $result[$i]['Branch_Code'];?></a></td>
<td class="text-right"><a href="javascript:void(0)" class="modaledit" data-id="<?php echo $result[$i]['Client_ID'];?>"><button class="btn btn-primary">Edit</button></a></td>
<td class="text-center"><input type="checkbox" name=<?php echo $result[$i]['Client_ID'];?>></td>
</tr>
<?php }
if(count($result)==0)
{
echo "<tr><td colspan='5' style='text-align:center; font-weight:bold;'>No data available</td></tr>";
}
else
{}
?>
</tbody>
<tfoot>
<tr>
<td colspan="8">
<div class="pagination pagination-centered"></div>
</td>
</tr>
</tfoot>
</table>
<script>
$('.modaldelete').click(function() {
$.pgwModal({
url: 'submit.php?action=delete&from=clients&col=Client_ID&id='+$(this).data('id'),
loading: '<span style="text-align:center">Loading in progress</span>',
close: false
});
});
</script>
这是我的 submit.php
if($_GET['action']=="delete")
{
$id = $_GET['id'];
$from = $_GET['from'];
$col = $_GET['col'];
$checkbox = $_POST['checkbox'];
for($i=0; $i<count($checkbox);$i++)
{
$del_id = $checkbox[$i];
$statement = "DELETE FROM `".$from."` WHERE `".$col."`='".$del_id."'";
$result = mysqlparser($statement, $db);
header("location:data_client.php?delete=1");
}
}
【问题讨论】:
标签: javascript php mysql checkbox pgwmodal