2016-08-23 44 views
-1

我建立了一個網站,其中有很多列表/表格(基於php)。我正在嘗試爲列表創建一個Change AllDelete 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按鈕使用相同的陣列。

+0

你很容易受到[sql注入攻擊](http://bobby-tables.com)的影響。 –

+0

而不是發送javscript到瀏覽器,並做一個完整的往返,爲什麼不使用PHP的'header('Location:another_page.php');' – RiggsFolly

+1

我是對的,假設你想要某人寫'Delete All'處理您? – RiggsFolly

回答

0

使用試錯法並嘗試兩個星期後,我終於找到了自己的解決方案,我有一個嚴重的抱怨,因爲沒有人從這個論壇關心我的解決方案。

很簡單。我所要做的就是用$_POST,而不是$_REQUEST並使用不同的名稱都喜歡這樣的按鈕:

<input class="w3-btn w3-green" type="submit" name="submit1" value="Change All"> 
<input class="w3-btn w3-red" type="submit" name="submit2" value="Delete All"> 

於是,我改變ajaxAction.php到:

<?php 
//Change All 
if(isset($_POST['submit1'])){ 
    if(!empty($_POST['chkbox'])){ 
     $targetpage = $_REQUEST['targetpage']; 
     foreach($_POST['chkbox'] as $selected){ 
      $sql_all = "update `table` set value='123' WHERE `id` = '$selected'"; 
      mysql_query($sql_all) or die(mysql_error()); 
     } 
    } 
?> 

<script> 
    location.href="tables.php?done"; 
</script> 
<?php } ?> 


<?php 
//Delete All 
if(isset($_POST['submit2'])){ 
    if(!empty($_POST['chkbox'])){ 
     foreach($_POST['chkbox'] as $selected){ 

      $sql_all = "DELETE FROM `table` WHERE `id` = '$selected'"; 
      mysql_query($sql_all) or die(mysql_error()); 
     } 
    } 
?> 

<script> 
    location.href="tables.php?done"; 
</script> 
<?php } ?> 

所有你所要做的就是告訴我應該使用$_POST。 Stack Overflow沒有人幫助我。

相關問題