我有一个简单的复选框示例,它记住在提交表单后选择的内容并且出现错误。
这部分效果很好......但是,我想将生成的“选中”框发布到我的表单邮件功能。
我现在只报告选中或未选中的内容,我希望在没有未选中框甚至注册的情况下拥有选中框的“值”。
<?php
$CB_1 = 'unchecked';
$CB_2 = 'unchecked';
$CB_3 = 'unchecked';
$CB_4 = 'unchecked';
$CB_5 = 'unchecked';
if (isset($_POST['submit'])) {
if (isset($_POST['CB_1'])) {$CB_1 = $_POST['CB_1'];
if ($CB_1 == 'item_01') {$CB_1 = 'checked';}
}
if (isset($_POST['CB_2'])) {$CB_2 = $_POST['CB_2'];
if ($CB_2 == 'item_02') {$CB_2 = 'checked';}
}
if (isset($_POST['CB_3'])) {$CB_3 = $_POST['CB_3'];
if ($CB_3 == 'item_03') {$CB_3 = 'checked';}
}
if (isset($_POST['CB_4'])) {$CB_4 = $_POST['CB_4'];
if ($CB_4 == 'item_04') {$CB_4 = 'checked';}
}
if (isset($_POST['CB_5'])) {$CB_5 = $_POST['CB_5'];
if ($CB_5 == 'item_05') {$CB_5 = 'checked';}
}
}
if (isset($_POST['submit'])) {
$error = "";
if (!empty($_POST['email'])) {
$email = $_POST['email'];
if (!preg_match("/^[a-z0-9]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){
$error .= "E-mail address not valid.";
}
} else {
$error .= "E-mail address is required.";
}
if (empty($error)) {
$from = 'From: '. @TEST .' <'. $email .'>';
$to = "someone@company.com";
$subject = "CHECKBOX TEST";
$content = "
checkbox selections:
check box 01: $CB_1
check box 02: $CB_2
check box 03: $CB_3
check box 04: $CB_4
check box 05: $CB_5
";
$success = mail($to,$subject,$content,$from);
}
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<?php if (!empty($error)) echo $error ?>
<br><br><br>
e-mail: <input type="text" name="email" value="<?php if (isset ($_POST {'email'})) { echo $_POST['email']; } ?>" />
<P>
<input type="checkbox" name="CB_1" value="item_01" <?PHP echo $CB_1; ?> /> Item 01
<input type="checkbox" name="CB_2" value="item_02" <?PHP echo $CB_2; ?> /> Item 02
<input type="checkbox" name="CB_3" value="item_03" <?PHP echo $CB_3; ?> /> Item 03
<input type="checkbox" name="CB_4" value="item_04" <?PHP echo $CB_4; ?> /> Item 04
<input type="checkbox" name="CB_5" value="item_05" <?PHP echo $CB_5; ?> /> Item 05
<P>
<input type="submit" name="submit" value="Submit"></input>
</form>