2013-10-22 75 views
0

在这里我想验证我的下拉框,这样如果select ...被选中,那么它将会出错。我已经这样做了,但是当表单被提交时,错误没有被捕获,只是提交。 ##只是为了澄清我有媒体链接累PUT 0,它仍然没有工作。##PHP验证错误

这里是我的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 = "[email protected]"; 
    $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._%-][email protected][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("[email protected]", $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 
} 
?> 
+0

@卢克Rixson:使用代替空isset。 –

+0

对不起,我忘了提及,我已经尝试过一个零,它仍然错误,我改变了这个字符串,看看它是否会工作 –

回答

0

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

+0

请参阅上面,因为我已经这样做,它仍然无法正常工作。你看到的是我的踪迹和错误的结果。 –

+0

而不是回显选择的食物,将其添加到错误消息变量。当你想提交时,检查error_message是否为空。如果是提交,否则不要提交 –

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.'; 
} 
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.');  
} 
0

你检查是否为选择提交的值是“食品... '但是您应该检查的值是'0'(在选择框的值中设置的数字,而不是标题)

请参阅下面的内容:

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

如果使用(空($ _POST [ 'formFood'])),如果值= 0则返回假

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)