看一下我写的blog.关于checkbox多选及反选的.
checkbox全选及反选

解决方案 »

  1.   


    这里省略生成html的代码,这个应该会撒。。读取数据库生成的页面代码如下
    将数据库中表主键的值作为checkbox的value
    注意checkbox name后面的[],必须要。。
    <form action="" method="post">
    <table>
    <tr>
    <td><input type="checkbox" id="chk[1]" name="chk[]" value="1" /></td>
    </tr>
    <tr>
    <td><input type="checkbox" id="chk[2]" name="chk[]" value="2" /></td>
    </tr>
    <tr>
    <td><input type="checkbox" id="chk[3]" name="chk[]" value="3" /></td>
    </tr>
    </table>
    <input type="submit" value="删除" />
    </form>删除调用的php代码
    if(isset($_POST['chk']))
    {
    $chk = $_POST['chk'];
    for($i=0,$j=count($chk);$i<$j;$i++)
    {
    $id=$chk[i];
    //操作数据库的代码
    //。
    }
    }
      

  2.   


    <SCRIPT LANGUAGE="JavaScript"><!-- Begin
    var checkflag = "false";
    function check(field) {
    if (checkflag == "false") {
    for (i = 0; i < field.length; i++) {
    field[i].checked = true;}
    checkflag = "true";
    return "false"; }
    else {
    for (i = 0; i < field.length; i++) {
    field[i].checked = false; }
    checkflag = "false";
    return "true"; }
    }
    //  End -->
    </script>
    <body></body>
    <form name="form" method="post" action=""><input type="checkbox" name="list" value="1"><input type="checkbox" name="list" value="2"><input type="checkbox" name="list" value="3"><br>
    全选<input type=checkbox value="全选" onClick="this.value=check(this.form.list)">
      

  3.   

    [Quote=引用 3 楼 hyying115 的回复:]php代码中 $id=$chk[i];  少写了个$
      

  4.   

    上面是查询db的代码。
    while ( $arr =mysql_fetch_array($res)) {

       echo " <input   type=checkbox name='id[ ]' value =".$arr['id']."> ";
    echo  "<input   type=submit   name=delete value =delete onclick =dateDel(".$arr['id'].") > ";
    }

    function dateDel(id){
    $arrId =$_REQUEST['id'];
    foreach($arrId as  $value){
    $sql1   =   "DELETE  *  FROM   syouhin   WHERE id=".$value;
    mysql_query($sql1);
    }
    }
    这个php代码能实现delete吗?
      

  5.   

    楼主的确够新的。。
    onclick =dateDel(".$arr['id'].")   汗一个一个是前台一个是后台啊。。
    根据你上面的代码简单粗略写了下  自己测试下。。
    <?php
    if(isset($_POST['id']))
    {
     $arrId =$_POST['id']; 
     foreach($arrId as  $value)

     $sql1  =  "DELETE  *  FROM  syouhin  WHERE id=".$value; 
     mysql_query($sql1); 


    ?>
    <html>
    <body>
    <form action="" method="post">
    <?php
    $res = mysql_query("SELECT * FROM syouhin");
    while ( $arr =mysql_fetch_array($res)) { 
      echo ' <input  type="checkbox" name='id[]' value ="'.$arr['id'].'" /> "; 
    }
    echo  '<input  type="submit"  name="delete" value="delete" /> '; 
    ?>
    </form>
    </body>
    </html>