防止改变输入值复选框

I have a form with multiple checkboxes that can be selected. If a user wanted they could go in and change the value in the checkbox. I want to know if there is a way I can not accept the form or display an error if the value submitted was not one of the original choices displayed. Here is my code:

HTML Code:

 <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 Code:

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

If you want, since html is static (which I dont know why), you could initialize some original values, then compare it. Consider this example:

<?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>

Note: To test, try to change the values thru the browser [most likely F12], change the dom values. Example: Try to change a checkbox value attribute to 7 or 100, check it the submit.

Also another way:

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

Print all the values inside a table:

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

}

Store values in a separate PHP file, checkbox_values.php:

<?php

$checkbox_values = array(1, 2, 3, 4, 5, 6);

?>

And your form, another PHP file, 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>

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

?>