<?php $conditions = array('price','color','metal'); //要進行篩選的字段放在這裏 $price = $color = $metal=''; //先給須要篩選的字段賦空值,這些值將輸出到頁面的hidden fileds中 //如下循環給已經進行的篩選賦值,以便可以在下一次篩選中保留 foreach($conditions as $value){ if(isset($_POST[$value])){ $$value = $_POST[$value]; } } //如下是演示輸出$_POST數據 echo '<pre>'; print_r($_POST); echo '</pre>'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>分類篩選演示</title> <style type="text/css"> body{font-size:14px;font-family:Tahoma,"宋體"} </style> <script type="text/javascript"> function Filter(a,b){ var $ = function(e){return document.getElementById(e);} var ipts = $('filterForm').getElementsByTagName('input'),result=[]; for(var i=0,l=ipts.length;i<l;i++){ if(ipts[i].getAttribute('to')=='filter'){ result.push(ipts[i]); } } if($(a)){ $(a).value = b; for(var j=0,len=result.length;j<len;j++){ if(result[j].value==''){ result[j].parentNode.removeChild(result[j]); } } document.forms['filterForm'].submit(); } return false; } </script> </head> <body> <form id="filterForm" action="listinfo.php" method="post"> <input to="filter" type="hidden" id="price" name="price" value="<?=$price?>" /> <input to="filter" type="hidden" id="color" name="color" value="<?=$color?>" /> <input to="filter" type="hidden" id="metal" name="metal" value="<?=$metal?>" /> </form> 價格:<a href="javascript:Filter('price','100-1000');">100-1000</a> <a href="javascript:Filter('price','1001-2000');">1001-2000</a> <a href="javascript:Filter('price','2001-3000');">2001-3000</a><br/> 顏色:<a href="javascript:Filter('color','紅色');">紅色</a> <a href="javascript:Filter('color','藍色');">藍色</a><br /> 材質:<a href="javascript:Filter('metal','純金');">純金</a> <a href="javascript:Filter('metal','純銀');">純銀</a><br /> </body> </html><br>// 將以上文件保存成listinfo.php運行就是篩選的效果!