PHP Checkbox没有填充数组

时间:2015-11-04 11:50:01

标签: php checkbox

我正在尝试创建一个修改用户表单,如果用户是管理员或者他们是活动的,则有两个复选框。

我已经阅读了数据库并相应地检查了复选框,但是当我尝试从userform获取值进行更新时,我的PHP只返回一个值。

<?php 
session_start(); 

include 'scripts/dbconnection.php';
include 'scripts/logoncheck.php';
include 'scripts/uservariables.php';

////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////Get operator number and name///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

$sql = "SELECT * from users";

$result = $mysqli->query($sql);

$displaytable = "<table>";
$displaytable = $displaytable."<thead><tr><th colspan='5'>Users!</th></tr>";
$displaytable = $displaytable."<tr><th>Number</th><th>Name</th><th>Admin</th><th>Active</th><th>Password</th></tr></thead><tbody>";

$user = array();

while($row = $result->fetch_assoc())
{
    $displaytable = $displaytable."<tr>";
    $displaytable = $displaytable."<td>".$row['opnum']."</td>";
    $displaytable = $displaytable."<td><input type='text' name='username[]' value='".$row['opname']."' required></td>";

    $admin = $row['admin'];

    if($admin == "Yes")
    {
        $displaytable = $displaytable."<td><input type='checkbox' name='admin[]' value='Yes' checked='checked'/></td>";
    }
    else
    {
        $displaytable = $displaytable."<td><input type='checkbox' name='admin[]' value='No'/></td>";
    }

    $active = $row['active'];

    if($active == "Yes")
    {
        $displaytable = $displaytable."<td><input type='checkbox' name='active[]' value='Yes' checked='checked'/></td>";
    }
    else
    {
        $displaytable = $displaytable."<td><input type='checkbox' name='active[]' value='No' /></td>";
    }
    $displaytable = $displaytable."<td><input type='checkbox' name='reset_password[]' /></td>";
    $displaytable = $displaytable."</tr>";
}

$displaytable = $displaytable."</tbody></table>";

$num_rows = mysqli_num_rows($result);


if(isset($_POST['submit']))
{
    $username = $_POST['username'];
    $useradmin = $_POST['admin'];
    $useractive = $_POST['active'];
    $resetpassword = $_POST['reset_password'];

    print_r($_POST['admin'][0]);
    print_r($_POST['admin'][1]);
    print_r($_POST['admin'][2]);

    for ( $i = 0; $i < $num_rows; $i++ ) 
    {
            echo "<br> User:".$username[$i]."<br>";
            if(isset($useradmin[$i]))
            {
                echo $username[$i]." is Admin <br>";
            }
            elseif(!isset($useradmin[$i]))
            {
                echo $username[$i]." is not Admin <br>";
            }

            if(isset($useractive[$i]))
            {
                echo $username[$i]." is Active <br>";
            }
            elseif(!isset($useractive[$i]))
            {
                echo $username[$i]." is not Active <br>";
            }           

            if(isset($resetpassword[$i]))
            {
                echo $username[$i]." reset password <br>";
            }
            elseif(!isset($resetpassword[$i]))
            {
                echo $username[$i]." not reset password <br>";
            }
    }
}

?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <LINK href="home.css" rel="stylesheet" type="text/css">
        <title>Add Results</title>
    </head>
    <body>
        <div class="login-card">                
            <?php include 'scripts/consts/menu.php';?>
            <h1>Add Results!</h1>
            <br>
            <div class="results1">
                <form action="#" method="POST">
                    <?php echo $displaytable;?>
                    <input name="submit" value="Save" type="submit" class="login login-submit">
                </form>
            </div>
        </div>
    </body>
</html>

目前从数组useractive打印的值只是:

Array ( [0] => Yes ) 

而且,我理想的是:

Array ( [0] => Yes, [1] => No, [2] => Yes) 

2 个答案:

答案 0 :(得分:0)

在您的代码中,如果数据库中的admin值为No,并且您想通过选中复选框并更新将值更改为Yes,则您将从复选框中获取No值

尝试使用以下方法替换while循环中的复选框代码:

$admin_checked = "";
if($admin == "Yes")
{
    $admin_checked = "checked='checked'";
}
$displaytable = $displaytable."<td><input type='checkbox' name='admin[]' value='Yes' $admin_checked /></td>";

$active = $row['active'];

$active_checked = "";
if($active == "Yes")
{
    $active_checked = "checked='checked'";
}
$displaytable = $displaytable."<td><input type='checkbox' name='active[]' value='Yes' $active_checked /></td>";

这样,如果值为Yes,则将检查复选框,您只需指定一次复选框输入代码

从复选框中获取值:

if(!($useradmin = $_POST['admin'])) //$_POST['admin'] is either "Yes" if checked or null if not checked
{
    $useradmin = "No";
}
if(!($useractive = $_POST['active']))
{
    $useractive = "No";
}

答案 1 :(得分:-1)

奇怪的是,如果已经选中复选框,您将只获得复选框的值。它看起来很愚蠢,但这就是它的工作方式。