防止改变输入值复选框

时间:2014-06-17 00:20:12

标签: php

我有一个可以选择多个复选框的表单。如果用户希望他们可以进入并更改复选框中的值。我想知道如果提交的值不是显示的原始选项之一,我是否有办法接受表单或显示错误。这是我的代码:

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; 
} 
}

2 个答案:

答案 0 :(得分:1)

如果你愿意,因为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>";

}

答案 1 :(得分:1)

将值存储在单独的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; } 
    } 
}

?>