Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP validation error
    primarykey
    data
    text
    <p>Here I want to validate my drop down box so that if select... is selected then it will error. I have done this but when the form is submitted the error is not caught and it just submits. ##JUST TO CLARIFY I HAVE ALLREADY TIRED TO PUT 0 AND IT STILL DID NOT WORK.##</p> <p>Here is my HTML</p> <pre><code> &lt;html&gt; &lt;head&gt; &lt;title&gt;Submit Form&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;p&gt;All fields marked with an asterisk "&lt;span style="color: red"&gt;*&lt;/span&gt;"are mandatory.&lt;/p&gt; &amp;nbsp; &lt;form name="SubmitForm" method="post" action="SubmitForm.php"&gt; &lt;table&gt; &lt;tr&gt; &lt;td valign="top" width=250px&gt; &lt;label for="first_name"&gt;&lt;span style="font-weight:bold"&gt;First Name &lt;span style="color: red"&gt;*&lt;/span&gt;&lt;/label&gt; &lt;/td&gt; &lt;td valign="top"&gt; &lt;input type="text" name="first_name" maxlength="50" size="30"&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td valign="top""&gt; &lt;label for="last_name"&gt;&lt;span style="font-weight:bold"&gt;Last Name &lt;span style="color: red"&gt;*&lt;/span&gt;&lt;/label&gt; &lt;/td&gt; &lt;td valign="top"&gt; &lt;input type="text" name="last_name" maxlength="50" size="30"&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td valign="top"&gt; &lt;label for="email"&gt;&lt;span style="font-weight:bold"&gt;Email Address &lt;span style="color: red"&gt;*&lt;/span&gt;&lt;/label&gt; &lt;/td&gt; &lt;td valign="top"&gt; &lt;input type="text" name="email" maxlength="80" size="30"&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td valign="top"&gt; &lt;label for="telephone"&gt;&lt;span style="font-weight:bold"&gt;Telephone Number &lt;span style="color: red"&gt;*&lt;/span&gt;&lt;/label&gt; &lt;/td&gt; &lt;td valign="top"&gt; &lt;input type="text" name="telephone" maxlength="30" size="30"&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td valign="top"&gt; &lt;label for="formFood"&gt;&lt;span style="font-weight:bold"&gt; What is your favourite food?&lt;span style="color: red"&gt;*&lt;/span&gt; &lt;/td&gt; &lt;td valign="top"&gt; &lt;select name="formFood" style="display:inline"&gt; &lt;option value="0"&gt;Select...&lt;/option&gt; &lt;option value="L"&gt;Lasagne&lt;/option&gt; &lt;option value="F"&gt;Fish and Chips&lt;/option&gt; &lt;option value="T"&gt;Toad in the Hole&lt;/option&gt; &lt;option value="S"&gt;Steak and Chips&lt;/option&gt; &lt;/select&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td height="30"&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2" style="text-align:center"&gt; &lt;input type="submit" value="Submit"&gt; &lt;a href="http://localhost/var/www/pages /SubmitForm.php"&gt; &lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>This is my PHP</p> <pre><code>&lt;?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.&lt;br /&gt;&lt;br /&gt;"; echo $error."&lt;br /&gt;&lt;br /&gt;"; echo "Please go back and fix these errors.&lt;br /&gt;&lt;br /&gt;"; 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.&lt;br /&gt;'; } $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.&lt;br /&gt;'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.&lt;br /&gt;'; } if(isset($_REQUEST['formFood']) &amp;&amp; $_REQUEST['formFood'] == '0') { echo 'Please select a Food.'; } if(strlen($telephone) &gt; 11) { died($error_message); } if(strlen($telephone) &lt; 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 "&lt;form method='post' action='mailform.php'&gt; Email: &lt;input name='email' type='text'&gt;&lt;br&gt; Subject: &lt;input name='subject' type='text'&gt;&lt;br&gt; Message:&lt;br&gt; &lt;textarea name='message' rows='15' cols='40'&gt; &lt;/textarea&gt;&lt;br&gt; &lt;input type='submit'&gt; &lt;/form&gt;"; } ?&gt; &lt;script&gt; alert("Thank you for contacting us. We will be in touch with you very soon.") &lt;/script&gt; &lt;?php } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload