0

在这里,我想验证我的下拉框,以便如果选择了 select... 则会出错。我已经这样做了,但是当表单提交时,错误没有被捕获,它只是提交。##只是为了澄清我已经厌倦了放置 0 并且它仍然不起作用。##

这是我的 HTML

    <html>
    <head>
        <title>Submit Form</title> 
    </head>

    <body>
    <p>All fields marked with an asterisk "<span style="color: red">*</span>"are mandatory.</p>
    &nbsp;
    <form name="SubmitForm" method="post" action="SubmitForm.php">
        <table>
        <tr>
             <td valign="top" width=250px>
             <label for="first_name"><span style="font-weight:bold">First Name <span style="color: red">*</span></label>
             </td>
                  <td valign="top">
             <input  type="text" name="first_name" maxlength="50" size="30">
             </td>
             </tr>
             <tr>
                 <td valign="top"">
                 <label for="last_name"><span style="font-weight:bold">Last Name <span style="color: red">*</span></label>
             </td>
     <td valign="top">
     <input  type="text" name="last_name" maxlength="50" size="30">
     </td>
     </tr>

     <tr>
         <td valign="top">
         <label for="email"><span style="font-weight:bold">Email Address <span style="color:    red">*</span></label>
         </td>
         <td valign="top">
         <input  type="text" name="email" maxlength="80" size="30">
         </td>
     </tr>
     <tr>
         <td valign="top">
         <label for="telephone"><span style="font-weight:bold">Telephone Number <span style="color: red">*</span></label>
         </td>
         <td valign="top">
         <input  type="text" name="telephone" maxlength="30" size="30">
         </td>
     </tr>
     <tr>
         <td valign="top">
         <label for="formFood"><span style="font-weight:bold"> What is your favourite food?<span style="color: red">*</span>
         </td>
         <td valign="top">
         <select name="formFood" style="display:inline">
                 <option value="0">Select...</option>
                 <option value="L">Lasagne</option>
                 <option value="F">Fish and Chips</option>
                 <option value="T">Toad in the Hole</option>
                 <option value="S">Steak and Chips</option>
         </select>
        </td>
     </tr>

     <tr>
         <td height="30">
         </td>
     </tr>

     <tr> 
         <td colspan="2" style="text-align:center">
         <input type="submit" value="Submit">   <a href="http://localhost/var/www/pages    /SubmitForm.php"> </a>
         </td>
     </tr>

       </table>
     </form>
   </body>
</html>

这是我的 PHP

<?php
          if(isset($_POST['email'])) {

    //Mail to this email
    $email_to = "submitform2@gmail.com";
    $email_subject = "Submit form";


    function died($error) {
        // Begin error code
        echo "We are very 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();
    }

    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['formFood'])) {
         died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // required
    $formFood = $_POST['formFood']; // required

    $error_message = "Please Enter correct values into all mandatory Feilds";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(isset($_REQUEST['formFood']) && $_REQUEST['formFood'] == '0') { 
  echo 'Please select a Food.'; 
} 

  if(strlen($telephone) > 11) {
    died($error_message); }
  if(strlen($telephone) < 11 ) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";

if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //send email
  mail("submitform2@gmail.com", $email_subject,
  $email_message, "From:" . $email);
  echo "Thank you for using our mail form";
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text'><br>
  Subject: <input name='subject' type='text'><br>
  Message:<br>
  <textarea name='message' rows='15' cols='40'>
  </textarea><br>
  <input type='submit'>
  </form>";
  }

?>
 <script>
alert("Thank you for contacting us. We will be in touch with you very soon.")
 </script>

<?php
}
?>
4

5 回答 5

0

use if (empty ($_POST ['formFood'])) if value is = 0 it will return false

 Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

The following things are considered to be empty:

    "" (an empty string)
    0 (0 as an integer)
    0.0 (0 as a float)
    "0" (0 as a string)
    NULL
    FALSE
    array() (an empty array)
    $var; (a variable declared, but without a value)
于 2013-10-22T11:35:46.447 回答
0

您不是在检查选项的值,而是检查选项的文本:

if(isset($_REQUEST['formFood']) && $_REQUEST['formFood'] == 'Select...') { 
  echo 'Please select a Food.'; 
} 

它一定要是:

if(isset($_REQUEST['formFood']) && $_REQUEST['formFood'] == '0') { 
  echo 'Please select a Food.'; 
} 
于 2013-10-22T11:31:27.820 回答
0

它应该是
if(isset($_REQUEST['formFood']) && $_REQUEST['formFood'] == 0) { echo 'Please select a Food.'; }

于 2013-10-22T11:32:17.267 回答
0

您正在检查提交的 select 值是否为“Food...”,但您应该检查的值是“0”(在选择框的值中设置的数字,而不是标题。

见下文:

 if(isset($_REQUEST['formFood']) && $_REQUEST['formFood'] == '0') { 
 echo 'Please select a Food.'; 
于 2013-10-22T11:32:48.060 回答
0

不要使用 ,而是为您不希望有效0的 使用空值,即。一般而言,这只是一个好习惯......如果您决定在某个时候包含前端验证,那么此配置也可以很好地开箱即用。<option><option value="">Select...</option>

当您检查 $_POST 变量时,请改用它:

// validation expected data exists
if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['telephone']) ||
    (!isset($_POST['formFood'] || empty($_POST['formFood']))) {
     died('We are sorry, but there appears to be a problem with the form you submitted.');      
}
于 2013-10-22T11:31:31.313 回答