KashMoney Posted December 6, 2007 Share Posted December 6, 2007 Hello, I have some issues with my php validation form. I need help with email field validation, Send the data to my email, and finally when submit have it say on the next page or even same page, Successfully sent. <? // Create an empty array to hold the error messages. $arrErrors = array(); //Only validate if the Submit button was clicked. if (!empty($_POST['Submit'])) { // Each time there?s an error, add an error message to the error array // using the field name as the key. if ($_POST['firstname']=='') $arrErrors['firstname'] = 'Please provide your first name.'; if ($_POST['lastname']=='') $arrErrors['lastname'] = 'Please provide your last name.'; if ($_POST['email']=='') $arrErrors['email'] = 'A valid email address is required.'; if ($_POST['subject']=='') $arrErrors['subject'] = 'Please enter a subject.'; if ($_POST['message']=='') $arrErrors['message'] = 'Please enter content in the message box.'; if (count($arrErrors) == 0) { // If the error array is empty, there were no errors. // Insert form processing here. } else { // The error array had something in it. There was an error. // Start adding error text to an error string. $strError = '<div class="formerror"><p>Please check the following and try again:</p><ol>'; // Get each error and add it to the error string // as a list item. foreach ($arrErrors as $error) { $strError .= "<li>$error</li>"; } $strError .= '</ol></div>'; } } ?> Form xhtml <fieldset id="emailform"> <p class="center"> If you want to contact me for anything regarding my site or any jobs that you need to be done. Please fill the form below and I will get back with you within 24 hours or less.</p> <?php echo $strError; ?> <form method="post" action="contact.html"> <!-- For every form field, we do the following... Check to see if there?s an error message for this form field. If there is, add the formerror class to the surrounding paragraph block. The formerror class contains the highlighted box. --> <p<?php if (!empty($arrErrors['firstname'])) echo ' class="formerrorl"'; ?>> <label for="firstname">* First Name:</label> <input name="firstname" type="text" id="firstname" size="30" value="<?php echo $_POST['firstname'] ?>"> </p> <p<?php if (!empty($arrErrors['lastname'])) echo ' class="formerrorl"'; ?>> <label for="lastname">* Last Name:</label> <input name="lastname" type="text" id="lastname" size="30" value="<?php echo $_POST['lastname'] ?>"> </p> <p<?php if (!empty($arrErrors['email'])) echo ' class="formerrorl"'; ?>> <label for="email">* Email Address:</label> <input name="email" type="text" id="email" size="30" value="<?php echo $_POST['email'] ?>"> </p> <p> <label>Website URL:</label> <input type="text" name="web" size="30" id="web" value="http://" /> </p> <p<?php if (!empty($arrErrors['subject'])) echo ' class="formerrorl"'; ?>> <label for="subject">* Subject:</label> <input name="subject" type="text" id="subject" size="30" value="<?php echo $_POST['subject'] ?>"> </p> <p<?php if (!empty($arrErrors['message'])) echo ' class="formerrorl"'; ?>> <label for="message">* Message:</label> <br /> <textarea rows="10" name="message" cols="85" id="message" value="<?php echo $_POST['message'] ?>"></textarea> </p> <p> <input type="submit" name="Submit" value="Submit"> <input type="reset" name="Reset" value="Reset"> </p> </form> <div class="italic">* required</div> </fieldset> Thanks in advance. KashMoney Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted December 6, 2007 Share Posted December 6, 2007 you can try this for email validation <?php if (!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $_POST['email'])) { echo 'Email entered is not valid'; } ?> Quote Link to comment Share on other sites More sharing options...
KashMoney Posted December 6, 2007 Author Share Posted December 6, 2007 I tried that and when i hit submit it doesn't display the email error! Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 6, 2007 Share Posted December 6, 2007 i know it might seem stupid but come people do do it but are you entering a invalid email? Quote Link to comment Share on other sites More sharing options...
KashMoney Posted December 6, 2007 Author Share Posted December 6, 2007 I simply left something out of the php side now it works! My mistake. Now just need help sending the data to my email and when someone clicks submit it says successfully sent. KashMoney Quote Link to comment Share on other sites More sharing options...
KashMoney Posted December 7, 2007 Author Share Posted December 7, 2007 I am not trying to bump up my post but I did the part where when someone click submit it sents me an email but the thing is php doesn't do any validation when I leave something blank on the form field. Here is the part that I did; <?php if(isset($_POST['submit'])) { $to = "myemail@name.com"; $subject = "Submission Form"; $fname_field = $_POST['firstname']; $lname_field = $_POST['lastname']; $email_field = $_POST['email']; $web_field = $_POST['web']; $subject_field = $_POST['subject']; $message = $_POST['message']; $body = " First Name: $fname_field\n Last Name: $lname_field\n E-Mail Address: $email_field\n Website URL: $web_field\n Subject: $subject_field\n Message: $message\n"; echo "<center>The message was sent successfully!</center>"; mail($to, $subject, $body); } else { echo "<center>Error Try Again!</center>"; } ?> Any help would be appreciated! Thanks, KashMoney Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted December 7, 2007 Share Posted December 7, 2007 You should do the validation stuff after the button is pressed i.e. <?php if(isset($_POST['submit'])) { // do your validation here if (error) { display error } else { continue with other code... } ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 7, 2007 Share Posted December 7, 2007 PHP doesn't do validation unless you put the code in to do it. Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted December 7, 2007 Share Posted December 7, 2007 PHP doesn't do validation unless you put the code in to do it. See the first post, he has done it Quote Link to comment Share on other sites More sharing options...
KashMoney Posted December 7, 2007 Author Share Posted December 7, 2007 n~ link=topic=170799.msg756271#msg756271 date=1197006354] You should do the validation stuff after the button is pressed i.e. <?php if(isset($_POST['submit'])) { // do your validation here if (error) { display error } else { continue with other code... } ?> Not sure what you mean on the code part? Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted December 7, 2007 Share Posted December 7, 2007 see this... hope you get the idea <?php # after user presses submit button..... if(isset($_POST['submit'])) { // check for validation .... if ($_POST['firstname']=='') $arrErrors['firstname'] = 'Please provide your first name.'; if ($_POST['lastname']=='') $arrErrors['lastname'] = 'Please provide your last name.'; if (!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $_POST['email'])) $arrErrors['email'] = 'A valid email address is required.'; if ($_POST['subject']=='') $arrErrors['subject'] = 'Please enter a subject.'; if ($_POST['message']=='') $arrErrors['message'] = 'Please enter content in the message box.'; // till here you did the validation # now check if error is empty or not, if empty do the mail if (count($arrErrors) == 0) // same as your code.... if errors are 0 process mail... { $to = "myemail@name.com"; $subject = "Submission Form"; $fname_field = $_POST['firstname']; $lname_field = $_POST['lastname']; $email_field = $_POST['email']; $web_field = $_POST['web']; $subject_field = $_POST['subject']; $message = $_POST['message']; $body = " First Name: $fname_field\n Last Name: $lname_field\n E-Mail Address: $email_field\n Website URL: $web_field\n Subject: $subject_field\n Message: $message\n"; echo "<center>The message was sent successfully!</center>"; mail($to, $subject, $body); } else { # there are some errors... display it echo "ERRORS OCCURED...."; } } ?> Quote Link to comment Share on other sites More sharing options...
KashMoney Posted December 7, 2007 Author Share Posted December 7, 2007 The thing is though my validation doesn't display on exactly which form is missing like i had it before. It highlights it. Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted December 7, 2007 Share Posted December 7, 2007 Try this now... php is case sensitive, so Submit and submit is different... you copy this all and see the changes... <?php $arrErrors = ""; # after user presses submit button..... if(isset($_POST['submit'])) { // check for validation .... if ($_POST['firstname']=='') $arrErrors= 'Please provide your first name.<BR>'; if ($_POST['lastname']=='') $arrErrors.= 'Please provide your last name.<BR>'; if (!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $_POST['email'])) $arrErrors.= 'A valid email address is required.<BR>'; if ($_POST['subject']=='') $arrErrors.= 'Please enter a subject.<BR>'; if ($_POST['message']=='') $arrErrors.= 'Please enter content in the message box.<BR>'; // till here you did the validation # now check if error is empty or not, if empty do the mail if (empty($arrErrors)) // same as your code.... if errors are 0 process mail... { $to = "myemail@name.com"; $subject = "Submission Form"; $fname_field = $_POST['firstname']; $lname_field = $_POST['lastname']; $email_field = $_POST['email']; $web_field = $_POST['web']; $subject_field = $_POST['subject']; $message = $_POST['message']; $body = " First Name: $fname_field\n Last Name: $lname_field\n E-Mail Address: $email_field\n Website URL: $web_field\n Subject: $subject_field\n Message: $message\n"; echo "<center>The message was sent successfully!</center>"; $sendmail = mail($to, $subject, $body); if (!$sendmail) { echo "Mail could not be sent";}; } else { # there are some errors... display it echo "ERRORS OCCURED...."; } } ?> <fieldset id="emailform"> <p class="center"> If you want to contact me for anything regarding my site or any jobs that you need to be done. Please fill the form below and I will get back with you within 24 hours or less.</p> <div style="color:#FF0000"><?php if (!empty($arrErrors)) {echo $arrErrors; } ?></div> <form method="post" action="contact.php"> <!-- For every form field, we do the following... Check to see if there?s an error message for this form field. If there is, add the formerror class to the surrounding paragraph block. The formerror class contains the highlighted box. --> <p> <label for="firstname">* First Name:</label> <input name="firstname" type="text" id="firstname" size="30" value="<?php if (isset($_POST['firstname'])) { echo $_POST['firstname']; } ?>"> </p> <p> <label for="lastname">* Last Name:</label> <input name="lastname" type="text" id="lastname" size="30" value="<?php if (isset($_POST['lastname'])) { echo $_POST['lastname']; } ?>"> </p> <p> <label for="email">* Email Address:</label> <input name="email" type="text" id="email" size="30" value="<?php if (isset($_POST['lastname'])) { echo $_POST['email']; } ?>"> </p> <p> <label>Website URL:</label> <input type="text" name="web" size="30" id="web" value="http://" /> </p> <p> <label for="subject">* Subject:</label> <input name="subject" type="text" id="subject" size="30" value="<?php if (isset($_POST['lastname'])) { echo $_POST['subject']; } ?>"> </p> <p> <label for="message">* Message:</label> <br /> <textarea rows="10" name="message" cols="85" id="message" value="<?php if (isset($_POST['lastname'])) {echo $_POST['message']; } ?>"></textarea> </p> <p> <input type="submit" name="submit" value="submit"> <input type="reset" name="Reset" value="Reset"> </p> </form> <div class="italic">* required</div> </fieldset> Quote Link to comment Share on other sites More sharing options...
KashMoney Posted December 7, 2007 Author Share Posted December 7, 2007 I see what you are trying to do, but I want it to go to another page with just the text saying sent successfully. When i do not put anything on the form I want it to highlight like a box in red then when you put something in then it goes away on submit. Not to be annoying. Not what I wanted done. I mean the idea of the stuff showing on top of the form of the missing stuff but the highlights are gone and the next page saying mail successfully gone is not in the code that you put out. Please bare with me. Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted December 7, 2007 Share Posted December 7, 2007 Please copy / paste this and check.... Did you mean like this.. To redirect to another page with your custom message, make that page and change the name of the file in header ("Location:mailsent.php"); <?php $arrErrors = array(); $my_css=""; $strError=""; # after user presses submit button..... if(isset($_POST['submit'])) { // check for validation .... if ($_POST['firstname']=='') $arrErrors['firstname']= 'Please provide your first name.<BR>'; if ($_POST['lastname']=='') $arrErrors['lastname']= 'Please provide your last name.<BR>'; if (!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $_POST['email'])) $arrErrors['email']= 'A valid email address is required.<BR>'; if ($_POST['subject']=='') $arrErrors['subject']= 'Please enter a subject.<BR>'; if ($_POST['message']=='') $arrErrors['message']= 'Please enter content in the message box.<BR>'; // till here you did the validation $my_css1 = (empty($_POST['firstname'])) ? 'input_err' : 'input_normal'; $my_css2 = (empty($_POST['lastname'])) ? 'input_err' : 'input_normal'; $my_css3 = (!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $_POST['email'])) ? 'input_err' : 'input_normal'; $my_css4 = (empty($_POST['subject'])) ? 'input_err' : 'input_normal'; $my_css5 = (empty($_POST['message'])) ? 'input_err' : 'input_normal'; # now check if error is empty or not, if empty do the mail if (count($arrErrors) == 0) // same as your code.... if errors are 0 process mail... { $to = "myemail@name.com"; $subject = "Submission Form"; $fname_field = $_POST['firstname']; $lname_field = $_POST['lastname']; $email_field = $_POST['email']; $web_field = $_POST['web']; $subject_field = $_POST['subject']; $message = $_POST['message']; $body = " First Name: $fname_field\n Last Name: $lname_field\n E-Mail Address: $email_field\n Website URL: $web_field\n Subject: $subject_field\n Message: $message\n"; $sendmail = mail($to, $subject, $body); echo "<center>The message was sent successfully!</center>"; if (!$sendmail) { echo "Mail could not be sent"; } else { header ("Location:mailsent.php"); exit; } } else { // The error array had something in it. There was an error. // Start adding error text to an error string. $strError = '<div class="formerror"><p>Please check the following and try again:</p><ol>'; // Get each error and add it to the error string // as a list item. foreach ($arrErrors as $error) { $strError .= "<li>$error</li>"; } $strError .= '</ol></div>'; } } ?> <style type="text/css"> <!-- .input_normal { background-color: #ffffff; } .input_err { background-color: #E49BA5; border: 2px solid #FF0000; } --> </style> <fieldset id="emailform"> <p class="center"> If you want to contact me for anything regarding my site or any jobs that you need to be done. Please fill the form below and I will get back with you within 24 hours or less.</p> <div style="color:#FF0000"><?php echo $strError; ?></div> <form method="post" action="contact.php"> <!-- For every form field, we do the following... Check to see if there?s an error message for this form field. If there is, add the formerror class to the surrounding paragraph block. The formerror class contains the highlighted box. --> <p> <label for="firstname">* First Name:</label> <input name="firstname" type="text" id="firstname" size="30" value="<?php if (isset($_POST['firstname'])) { echo $_POST['firstname']; } ?>" class="<?php echo $my_css1; ?>" > </p> <p> <label for="lastname">* Last Name:</label> <input name="lastname" type="text" id="lastname" size="30" value="<?php if (isset($_POST['lastname'])) { echo $_POST['lastname']; } ?>" class="<?php echo $my_css2; ?>"> </p> <p> <label for="email">* Email Address:</label> <input name="email" type="text" id="email" size="30" value="<?php if (isset($_POST['lastname'])) { echo $_POST['email']; } ?>" class="<?php echo $my_css3; ?>"> </p> <p> <label>Website URL:</label> <input type="text" name="web" size="30" id="web" value="http://" /> </p> <p> <label for="subject">* Subject:</label> <input name="subject" type="text" id="subject" size="30" value="<?php if (isset($_POST['lastname'])) { echo $_POST['subject']; } ?>" class="<?php echo $my_css4; ?>"> </p> <p> <label for="message">* Message:</label> <br /> <textarea rows="10" name="message" cols="85" id="message" class="<?php echo $my_css5; ?>"><?php if (isset($_POST['lastname'])) {echo $_POST['message']; } ?></textarea> </p> <p> <input type="submit" name="submit" value="submit"> <input type="reset" name="Reset" value="Reset"> </p> </form> <div class="italic">* required</div> </fieldset> Quote Link to comment Share on other sites More sharing options...
KashMoney Posted December 7, 2007 Author Share Posted December 7, 2007 Thank you all for your help especially ~n[EO]n~ you have been really helpful. KashMoney Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.