javascript+php 实现全选/全不选 以及对checkbox的处理

<?php
//connect to database
function db_link()
{
   $access_id   = "root";
   $accesss_pwd = "831025";
   $db_name     = "inv";
   @ $db = mysql_connect('localhost', $access_id, $accesss_pwd) or
                die("Could not connect to database. Please contact with IT supporting team ASAP.");
    mysql_query("SET NAMES 'GBK'");
    mysql_select_db($db_name);
    return $db;
}
$link = db_link();
?>

<!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=gb2312" />
<title>select all/unselect all</title>
<script type="text/javascript">
 function setCheckboxesRange(the_form, do_check, basename, min, max){
     for (var i = min; i <= max; i++) {
         if (typeof(document.forms[the_form].elements[basename + i]) != 'undefined') {
             document.forms[the_form].elements[basename + i].checked = do_check;
         }
     }
     return true;
 }
</script>
</head>
<body>

<?php
$sn_plant = $_POST['sn_plant'];
$sn_sales = $_POST['sn_sales'];
?>

<form name="form1" action="" method="post">
<fieldset>
 <legend>criteria</legend>
 <table cellpadding="0" cellspacing="1">
  <tr><th>Plant:</th>
   <td>
       <input type="text" name="sn_plant" value="<?php echo $sn_plant;?>" maxlength="12" />
   <td>
  </tr>
  <tr><th>Sales:</th>
   <td>
       <input type="text" name="sn_sales" value="<?php echo $sn_sales;?>" maxlength="12" />
          <input type="submit" name="search" value="Search" />
   <td>
  </tr>
 </table>
</fieldset>

<?php
if($_POST['search']=='Search'){
 $sn_sql   = "SELECT sn_id,sn_plant,sn_sales FROM sn WHERE sn_plant='".$sn_plant."' AND      sn_sales='".$sn_sales."' ";
 $sn_res   = mysql_query($sn_sql);
 $sn_num   = mysql_num_rows($sn_res);
 
 if($sn_num==0){
  echo  '<font color="red">Record doesn/'t exsit.</font>';
 }else{
     echo'<fieldset><legend>list</legend>
      <table border=1>
      <tr><th>chk</th><th>id</th><th>plant</th><th>sales</th></tr>'; 
     for($i=0; $i<$sn_num; $i++){
       $sn_row= mysql_fetch_array($sn_res); 
         echo '<tr>';
         echo '<td><input type="checkbox" name="sn['.$i.']" id="rows'.$i.'" value="'.$sn_row['sn_id'].'" ></td>';
         echo '<td>'.$sn_row['sn_id'].'</td>';
        echo '<td>'.$sn_row['sn_plant'].'</td>';
        echo '<td>'.$sn_row['sn_sales'].'</td>';
        echo '</tr>';
     }
     echo ' <input type="hidden" name="sn_plant" value="'.$sn_plant.'">
               <input type="hidden" name="sn_sales" value="'.$sn_sales.'">
              </table>
             </fieldset>
            <a href="#" οnclick="setCheckboxesRange(/'form1/', true, /'rows/', 0, '.$i.'); return false;">全选</a>/
           <a href="#" οnclick="setCheckboxesRange(/'form1/', false, /'rows/', 0, '.$i.'); return false;">全不选</a>
          <input type="submit" name="add" value="Add">';
 }
 echo '</form>';
}

if($_POST['add']=='Add'){
      $sn              =  $_POST['sn'];
      $sn_plant   =  $_POST['sn_plant'];
      $sn_sales  =  $_POST['sn_sales'];
   
 //if user didn't choose it ,it's value is null,otherwise it's value equals sn_id.
 //for checkbox,what we need to do is give it an unique value ,then deal with it in array.

 echo 'You have chosen following items:';
 foreach($sn as $key => $value){
  if($sn[$key] != ""){
      echo $sn[$key].',';
  }
 }
}
?>

</body>
</html> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值