我有一个带有选项菜单的页面,但是希望用户通过验证码测试。目前我想要它,以便用户在文本框中输入至少 3 个字符,否则他们将无法提交表单,代码如下所示,表单末尾的 php 条件。但是我知道我显然弄错了。有没有办法做到这一点而不将 php 代码粘贴到另一个页面上?谢谢。
<?php
// Check to see if the form has been submitted.
if(isset($_POST['menu1'])) {
// If the form has been submitted, force a re-direct to the choice selected.
header('Location: ' . $_POST['menu1']);
}
?>
<div style="padding-left: 50px">
<p class="arial"><strong></strong><br /><br /></p>
<form method="post">
<table class="freecontact2form" border="0" width="400px">
<tbody>
<tr>
<td colspan="2"><span style="font-size: x-small;"> </span> <font color=#000000 >Which of the following do you want to use?</font>
<br /><br /></td>
</tr>
<tr>
<td valign="top"><table width="400px" class="freecontact2form">
<tr>
<td colspan="2"><br />
<br />
<div class="freecontact2formmessage"> </div></td>
</tr>
<tr>
<td valign="top"><label for="menu1" >The options are:<span class="required_star"> </span></label></td>
<td valign="top"><select name="menu1" id="menu1">
<option selected="selected" value ="http://www.google.com">Google </option>
<option value ="http://www.yahoo.com">Yahoo</option>
<option value ="http://www.bing.com">Bing</option>
</select></td>
</tr>
<tr>
<td valign="top"><label for="captcha" ><span class="required_star"></span><span class="required_star"></span></label></td>
<td valign="top"><BR /><BR /><img src="captcha.jpg" /></td>
</tr>
<tr>
<td>Please enter the characters shown in the CAPTCHA image:</td>
<td><input type="text" name="captcha" id="captcha" value="" size="10" />
</td>
</tr>
<tr>
<td style="text-align:center" colspan="2"><br /><br />
<input type="submit" value=" Submit ">
</td>
</tr>
</table></td>
<td valign="top"> </td>
</tr>
</tbody>
</table>
</form> <br />
<p> </p>
<p> </p>
<?php
if(isset($_POST['menu1'])) {
function died($error) {
echo "Sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
if(!isset($_POST['menu1']) ||
!isset($_POST['captcha'])
) {
died('Sorry, there appears to be a problem with your form submission.');
}
$error_message = "";
if(strlen($captcha) < 3) {
$error_message .= 'Please ensure the captcha entered is at least 3 characters long';
}
if(strlen($error_message) > 0) {
died($error_message);
}
?>
<?php
}
die();
?>