提交php表单前的条件

提交php表单前的条件,php,html,captcha,Php,Html,Captcha,我有一个选项菜单页面,但希望用户通过验证码测试。目前,我想要它,以便用户在文本框中输入至少3个字符,否则他们将无法提交表单,下面显示的代码在表单末尾的php条件。然而,我知道我显然弄错了。是否有任何方法可以在不将php代码粘贴到其他页面的情况下执行此操作?谢谢 <?php // Check to see if the form has been submitted. if(isset($_POST['menu1'])) { // If the form has been submitt

我有一个选项菜单页面,但希望用户通过验证码测试。目前,我想要它,以便用户在文本框中输入至少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">&nbsp;</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();
?>



您希望使用以下哪项?



这些选择包括: 谷歌 雅虎 宾

请输入验证码图像中显示的字符:



在文件顶部(而不是底部)尝试此代码。注意,我修复了一些问题

首先,在运行验证代码之前,您正在执行header()重定向,因为当验证在下面时,它位于文件的顶部。我更改了逻辑,因此如果验证成功,它将重定向,否则将显示错误消息

第二,您正在检查strlen($captcha),但应该检查
strlen($\u POST['captcha'))


您必须首先将验证码保留在会话中,然后将其与提交的值进行检查,以便在验证输入的值是否为3个字符或更多字符后与之匹配

<?php
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'])) {
  if(!isset($_POST['menu1']) || !isset($_POST['captcha'])) {
      died('Sorry, there appears to be a problem with your form submission.');        
  }

  $error_message = "";

  if(strlen($_POST['captcha']) < 3) {
    $error_message .= 'Please ensure the captcha entered is at least 3 characters long';
  }

  if(strlen($error_message) > 0) {
    // VALIDATION ERRORS, DIE
    died($error_message);
  }
  else {
    // DO YOUR REDIRECT RIGHT HERE... NO VALIDATION ISSUES, CONTINUE...
    header('Location: ' . $_POST['menu1']);
  }
}
?>