-1
我建立了一个网站,其中有很多列表/表格(基于php)。我正在尝试为列表创建一个Change All
和Delete All
系统。我已将checkbox
添加到所有单个列表项。我也有一个复选框,只需点击一下就可以选择所有这些项目。现在我被困在如何执行更改和删除选项。已经过了3天...更改和删除列表中的选定项目 - PHP
- 我已经做了
Change All
的代码了。但我不明白Delete All
部分。 - 我不知道要传递$ targetpage变量以使其重定向。
下面是代码:
<!DOCTYPE html>
<html>
<title>List 1</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3-theme-purple.css">
<?php include('dbcon.php');?>
<style>a {text-decoration: none;}</style>
<!--Select All-->
<script>
function checkAll(ele) {
var checkboxes = document.getElementsByTagName('input');
if (ele.checked) {
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].type == 'checkbox') {
checkboxes[i].checked = true;
}
}
} else {
for (var i = 0; i < checkboxes.length; i++) {
console.log(i)
if (checkboxes[i].type == 'checkbox') {
checkboxes[i].checked = false;
}
}
}
}
</script>
<body>
<!--?php include ('header.php');?-->
<form name="form1" enctype="multipart/form-data" action="ajaxAction.php" method="post">
<div class="w3-container">
<div class="w3-padding-32 w3-center">
<input class="w3-btn w3-green" type="submit" name="chkbox" value="Change All" />
<input class="w3-btn w3-red" type="submit" value="Delete All">
</div>
<center>
<table class="w3-table-all" style="width: auto;">
<tr class="w3-theme">
<th><input class="w3-check" type="checkbox" onchange="checkAll(this)" name="chk[]" /></th>
<th>Data</th>
</tr>
<tr>
<td>
<input class="w3-check" type="checkbox" name="chkbox[]" />
</td>
<td class="w3-small" >
The data is displayed here.
</td>
</tr>
</table>
</center>
</div>
</form>
<br>
<!--?php include ('footer.php');?-->
</body>
</html>
这里是ajaxAction.php代码:
<!--Change-->
<?php
if ($_REQUEST['chkbox']) {
$targetpage = $_REQUEST['targetpage'];
$change = array();
$change = $_REQUEST['chkbox'];
for($c=0;$c<count($change); $c++){
$sql_all = "update `table` set `value`='123' WHERE `id` = '$change[$c]'";
mysql_query($sql_all) or die(mysql_error());
}
?>
<script>
var targetpage = "<?php echo $targetpage ?>";
location.href=targetpage+"?done";
</script>
<?php } ?>
<!--Delete-->
<?php ?>
<script>
var targetpage = "<?php echo $targetpage ?>";
location.href=targetpage+"?done";
</script>
随着我当前的代码,我只能够使用所选阵列chkbox[]
为一个处理即Change All
。我不知道如何对Delete All
按钮使用相同的阵列。
你很容易受到[sql注入攻击](http://bobby-tables.com)的影响。 –
而不是发送javscript到浏览器,并做一个完整的往返,为什么不使用PHP的'header('Location:another_page.php');' – RiggsFolly
我是对的,假设你想要某人写'Delete All'处理您? – RiggsFolly