2013-11-03 101 views
0

我有幾個複選框過濾數據庫的結果,但我有兩個問題,第一個是我使用$_POST變量,當我點擊刷新時,我得到一個確認表單提交對話框,其中我不要因爲用戶可能會多次刷新。複選框過濾不起作用

第二部分是,如果我用$_GET替換$_POST,我沒有看到確認消息,但複選框不起作用。任何人有任何想法?

<script type="text/javascript"> 
    $(function(){ 
    $('.checkbox').on('change',function(){ 
     $('#form').submit(); 
     }); 
    }); 
    </script> 


    <form id="id" method="post" action=""> 
<input type="checkbox" name="new" class="checkbox" <?php if(isset($_POST['new'])) echo "checked"; ?>/> New<br> 
<input type="checkbox" name="used" class="checkbox" <?=(isset($_POST['used'])?' checked':'')?>/> Used<br> 
<input type="checkbox" name="ref" class="checkbox" <?=(isset($_POST['ref'])?' checked':'')?>/> Refurbished<br> 
</form> 

if(isset($_GET['page'])) 
     { 
      $page = $_GET['page']; 
     } 
     else 
     { 
      $page = 1; 
     } 
     $options = array(
      'results_per_page'    => 2, 
      'url'       => 'products.php?search=' . urlencode($q) . '&amp;page=*VAR*', 
      'db_handle'      => $dbh 
     ); 
      if (isset($_POST["ref"])) { 
      $arguments[] = " condition LIKE '%refur%' "; 
      } 
      if (isset($_POST["new"])) { 
      $arguments[] = " condition LIKE '%new%' "; 
      } 
      if (isset($_POST["used"])) { 
      $arguments[] = " condition LIKE '%use%' "; 
      } 
      if(!empty($arguments)) { 
      $str = implode(' or ',$arguments); 
      $qry = "SELECT * FROM products where " . $str . " ORDER BY id desc"; 
      $paginate = new pagination($page, $qry, $options); 
      echo $qry; 
      } 
      else { 

      $paginate = new pagination($page, "SELECT * FROM products order by id desc", $options); 
      } 

回答

0

這只是一個想法,使用古典/現代阿賈克斯方法提交。假設這個頁面被稱爲jqpost.php:

<script> 
$(document).ready(function() { 
    $('.checkbox').on('change',function(){ 
    //$('#form').submit(); 
    var formdata = $("#form").serialize(); 
    $.post("jqpost.php", formdata, function(data) { 
     $("#result").html("Post OK: " + formdata); 
    }); 
    }); 
}); 
</script> 

<form id="form" method="post" action=""> 
<input type="checkbox" name="new" class="checkbox" <?php if(isset($_POST['new'])) echo "checked"; ?>/> New<br> 
<input type="checkbox" name="used" class="checkbox" <?=(isset($_POST['used'])?' checked':'')?>/> Used<br> 
<input type="checkbox" name="ref" class="checkbox" <?=(isset($_POST['ref'])?' checked':'')?>/> Refurbished<br> 
</form> 

<div id="result"></div> 

<?php 
//... 
?> 

如果用戶點擊一個複選框,jQuery將數據公佈至jqpost.php腳本。作爲回報,#result div將提供狀態文本。

因此,如果用戶按下瀏覽器上的刷新按鈕,表單不會一再提交。