防止更改输入值复选框


preventing altered input value checkbox

我有一个表单,可以选择多个复选框。如果用户愿意,他们可以进入并更改复选框中的值。我想知道是否有一种方法,我不能接受的形式或显示错误,如果提交的值不是显示的原始选择之一。下面是我的代码:

HTML代码:

 <form action="" method="post">
<table>
<tr>
<td>
<input type="checkbox" name="checkbox[]" value="1">
</td>  
<td>
<input type="checkbox" name="checkbox[]" value="2">
</td>
   <td>
<input type="checkbox" name="checkbox[]" value="3">
</td>
   <td>
<input type="checkbox" name="checkbox[]" value="4">
</td>
      <td>
<input type="checkbox" name="checkbox[]" value="5">
</td>
      <td>
<input type="checkbox" name="checkbox[]" value="6">
      </td>
</tr>

</table>
   <input type="submit" name="submit" value="submit">
</form>
PHP代码:

<?php 
if(!empty($_POST['checkbox'])) { 
foreach($_POST['checkbox'] as $check) { 
        echo $check; 
} 
}

如果你想,因为html是静态的(我不知道为什么),你可以初始化一些原始值,然后比较它。考虑这个例子:

<?php
$original_values = array(1, 2, 3, 4, 5, 6);
if(isset($_POST['submit'], $_POST['checkbox'])) {
    $submitted_values = $_POST['checkbox'];
    foreach($submitted_values as $value) {
        if(!in_array($value, $original_values)) {
            // this particular submitted value does not belong to the original values that is set
            echo 'not good';
            exit;
        }
    }
    // all good if it goes down here
    echo 'all good';
}
?>
<form method="POST" action="">
    <table border="0" cellpadding="10">
        <tr>
            <?php foreach($original_values as $value): ?>
                <td><input type="checkbox" name="checkbox[]" value="<?php echo $value; ?>" /></td>
           <?php endforeach; ?>
        </tr>
        <tr><td colspan="6"><input type="submit" name="submit" value="submit" /></td></tr>
    </table>
</form>

注意:要测试,尝试通过浏览器更改值[最有可能的F12],更改dom值。示例:尝试将复选框值属性更改为7或100,检查是否提交。

$original_values = array(1, 2, 3, 4, 5, 6);
if(isset($_POST['submit'], $_POST['checkbox'])) {
    $submitted_values = $_POST['checkbox'];
    $differences = array_diff($submitted_values, $original_values);
    if(count($differences) > 0) {
        echo 'not good';
        exit;
    }
    // all good if it goes down here
    echo 'all good';
}

打印表中的所有值:

if(isset($_POST['submit'], $_POST['checkbox'])) {
    $submitted_values = $_POST['checkbox'];
    $count = count($submitted_values);
    echo "<table border='1' cellpadding='10'>";
    echo "<tr><td colspan='$count'>Values</td></tr>";
    echo "<tr>";
    foreach($submitted_values as $value) {
        echo "<td>$value</td>";
    }
    echo "</tr>";
    echo "</table>";
}

将值存储在单独的PHP文件checkbox_values.php:

<?php
$checkbox_values = array(1, 2, 3, 4, 5, 6);
?>

表单,另一个PHP文件main。PHP:

<?php
require 'checkbox_values.php';
?>
<form method="POST" action="checkbox_process.php">
    <table border="0" cellpadding="10">
        <tr>
<?php foreach ( $checkbox_values as $check ) { ?>
            <td><input type="checkbox" name="checkbox[]" value="<?php echo $check; ?>" /></td>
<?php } ?>
        </tr>
        <tr><td colspan="6"><input type="submit" name="submit" value="submit" /></td></tr>
    </table>
</form>

和checkbox_process.php:

<?php
require 'checkbox_values.php';
if(!empty($_POST['checkbox'])) { 
    foreach($_POST['checkbox'] as $check) { 
        if ( in_array($check, $checkbox_values) ) { echo $check; } 
    } 
}
?>