-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