如何获取php中的多个复选框的值 [英] how to get the value of multiple checkbox in php

查看:85
本文介绍了如何获取php中的多个复选框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码为所选的复选框选择多个值从数据库表中删除它,但是当我print_r它只显示键,因为它没有显示值的id。

I have use the following code to select multiple value for selected checkbox to delete it from database table but when I am print_r its only shows keys for it not showing value for id.

我使用此代码在数组中获取值: -

I have use this code to get value in array:-

<?php
echo "Hiiiiiiiiii";
include("conn.php");
$sql="select * from test ";

$res=mysql_query($sql) or die(mysql_error());
?>

<form name="form1" method="POST" action="">
    <table width="578" border="1" align="center" id="menu">
    <tr>
    <th></th>
    <th>id</th>
    <th>Name</th>
    <th>email</th>
    <th>phno</th>
 </tr>

<?php
 while($row=mysql_fetch_array($res))
 {
?>

 <tr>

    <td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
    <td><?php echo $row['id'];?></td>

    <td><?php echo $row['name'];?></td>
    <td><?php echo $row['emailid'];?></td>

    <td><?php echo $row['phno'];?></td>
    <?php
    echo"<td><a href='update.php?id=".$row['id']."'>Update</a></td>";
    ?>
 <?php
  }
 ?>  
 <tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table>

 <?php
// Check if delete button active, start this
$count = mysql_num_rows($res);
echo "$count";

if(isset($_POST['delete']))
{
    if(count($_POST['checkbox']) !=0)
    {
        $array = array("checkbox" => $_POST['checkbox']);
        print_r($array);
        $ids = implode(',', $array);
        echo "$ids";
        $result = mysql_query("DELETE FROM test WHERE id IN ($ids)") or die(mysql_error());
    }
}
// if successful redirect to delete_multiple.php 
if($result)
    {
     echo "<meta http-equiv=\"refresh\" content=\"0;URL=teach_delinquent.php\">";
     echo "SUCCESS";
    }

 ?>


推荐答案

快速解决您的问题如下

替换下面的行

$array = array("checkbox" => $_POST['checkbox']);

$array = $_POST['checkbox'];

$array = array_map('intval',$_POST['checkbox']); // if all values are integer ---for security

因为$ _POST ['checkbox']已经是一个数组

because $_POST['checkbox'] is already an array

希望这将解决您的问题

这篇关于如何获取php中的多个复选框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆