| | |
Help with Implementing email form validations please
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Hi there I am currently completing a uni assignment using php and I thought it was finished until the lecturer dropped a bombshell and added another requirement. I have a contact us page which was basically just a shell (dead) page, he nows tell us that we have to do error validating on the form input with appropriate error messages, as well as echoing to the screen a message acknowledging receipt of the message when it is submitted. I am just not sure how to implement this. I am basically working with 3 files as well as the formatting files and the css, there is the contact.php, which is the basic web page, it calls in the other two files which are the includes/enquiryform.php and the includes/enquiryCheck.php. The page and the form work perfectly, but the error checking doesn't seem to do anything at all.
I will post all the code below hoping that someone can help me out of this very frustrating jam as this assignment is due on sunday and I have been working at this one final problem for 21 hours straight now and can't even think coherently any more. Oh some stuff id commented out, I have just been trying anything to try and make it work.
Also if anyone is genuinely interested in helping me out I will give you the url of where the site can be located, I am just not keen on revealing that on a public forum as it is a uni assignment and I don't want anyone copying it right now.
Cheers and sorry for rambling on I am just so very tired.
Giddyupgirl
contact.php
includes/enquiryCheck.php
includes/enquiryform.php
I will post all the code below hoping that someone can help me out of this very frustrating jam as this assignment is due on sunday and I have been working at this one final problem for 21 hours straight now and can't even think coherently any more. Oh some stuff id commented out, I have just been trying anything to try and make it work.
Also if anyone is genuinely interested in helping me out I will give you the url of where the site can be located, I am just not keen on revealing that on a public forum as it is a uni assignment and I don't want anyone copying it right now.
Cheers and sorry for rambling on I am just so very tired.
Giddyupgirl
contact.php
PHP Syntax (Toggle Plain Text)
<?php //session_start(); // relevant variables for the header $page_title = "School Friends Club - Contact Us"; $page_meta_description = "School Friends Club contact us"; $page_meta_revised = "******* - 16/08/2009"; $page_name = "pageContact"; require ('includes/checkEnquiry.php'); //checkEnquiry(); require ('includes/navigation.php'); ?> <!-- Content Div --> <div id="content"> <p><img src="images/contact title.jpg" alt="Contact Us" width="720" height="60" /> <img src="images/fancy67.gif" alt="bar" width="720" height="6" /></p> <!-- Contact Div - Left-side Pane --> <div id="contact"> <h3> The School Friends Club</h3> <p> Lourdes College, Traralgon Campus<br /> Building 12A, Level 2, Room 7<br /> 345 - 399 Grey Street <br /> PO Box 783 <br /> Traralgon VIC 3844 Australia <br /> Phone: +61 7 4651 5555<br /> Fax: +61 7 4651 7777<br /> Email: <a href="mailto:admin@sfc.com.au">admin@sfc.com.au</a></p> </div> <!-- close contact div--> <!-- Enquiry Div - Right-side Pane --> <?php require ('includes/enquiryform.php'); ?> </div> <!-- close content div--> <?php require ('includes/footer.php'); ?> </div> <!-- close page div --> </div> <!-- close container div --> </body> </html>
includes/enquiryCheck.php
PHP Syntax (Toggle Plain Text)
<?php // variable for any errors $errors; /*function checkEnquiry()*/ { // if a user has just clicked submit, these will be set if (isset($_POST["submit"])) { // create variables to hold the data$name = $_POST['name'] ; $name = $_POST['name'] ; $email = $_POST['email'] ; $question = $_POST['question'] ; // if the all fields contain data if (!empty($name)&& !empty($email)&& !empty($question)) { // Make sure the name contains only letters if (ereg('[^a-zA-Z]+', $name)) { $errors .= '<h3>Name can only contain letters.</h3>'; } if (ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { $errors .= '<h3> Not a valid email address </h3>'; } // Make sure the question contains only alphanumeric and numbers if (ereg('[^A-Za-z0-9]+', $question)) { $errors .= '<p>Your question can only contain letters and numbers.</p>'; } } else { // if the name is empty, create error if (empty($name)) { $errors .= '<p>Name field cannot be empty.</p>'; } // if the email is empty, create error if (empty($email)) { $errors .= '<p>Email field cannot be empty.</p>'; } // if the question is empty, create error if (empty($question)) { $errors .= '<p>Question field cannot be empty.</p>'; } /*// if errors were created, print them to the screen if(empty($errors)) { echo '<h3>Your email was sent successfully</h3>'; }*/ } } } ?>
includes/enquiryform.php
PHP Syntax (Toggle Plain Text)
<div id="enquiry"> <form action="mailto:admin@******.net" method="post"> <fieldset class="enquiryForm"> <legend class="legendHeader">Enquiry Form</legend> <br /> <label for="name">Name:</label> <input type="text" id="name"></input><br /> <label for="email">Email:</label> <input type="text" id="email" ></input><br /> <label for="question">Question:</label> <textarea rows="3" cols="20" id="question"></textarea> <br /><br /> <input type="submit" value="Submit"></input> <input type="reset" value="Reset"></input> </fieldset> </form> </div> <!-- close enquiry div-->
Last edited by giddyupgirl; Aug 14th, 2009 at 6:56 am.
•
•
Join Date: Apr 2008
Posts: 53
Reputation:
Solved Threads: 10
•
•
•
•
PHP Syntax (Toggle Plain Text)
// if errors were created, print them to the screen if(empty($errors)) { echo '<h3>Your email was sent successfully</h3>'; }
PHP Syntax (Toggle Plain Text)
else { echo '<h3>Errors were detected</h3>'.$errors; }
If your validations aren't working, please reply with details of what is wrongly passing through the validations, so we can take a closer look at them
Hi Simon,
Thank you so much for your reply, it is basically that absolutely nothing is happening, the errors aren't caught and no error messages are displayed. It is as if the enquiryCheck isn't even there or implementing. Sorry I am probably not making much sense but I don't have the skill to use the correct terminology.
thank you
Fiona
PS I have added that snippet to the code and still nothing happens.
Thank you so much for your reply, it is basically that absolutely nothing is happening, the errors aren't caught and no error messages are displayed. It is as if the enquiryCheck isn't even there or implementing. Sorry I am probably not making much sense but I don't have the skill to use the correct terminology.
thank you
Fiona
PS I have added that snippet to the code and still nothing happens.
Last edited by giddyupgirl; Aug 14th, 2009 at 7:50 am.
•
•
Join Date: Apr 2008
Posts: 53
Reputation:
Solved Threads: 10
The bit in your code that echoes whether or not the email was sent seems to be commented out. Have you removed the comments?
If you remove the commenting, so your code is like below, what is echoed to the screen? If the function is being called correctly, you should get either of the two possible outputs below.
PHP Syntax (Toggle Plain Text)
/*// if errors were created, print them to the screen if(empty($errors)) { echo '<h3>Your email was sent successfully</h3>'; }*/
If you remove the commenting, so your code is like below, what is echoed to the screen? If the function is being called correctly, you should get either of the two possible outputs below.
PHP Syntax (Toggle Plain Text)
if(empty($errors)) { echo '<h3>Your email was sent successfully</h3>'; } else { echo '<h3>Errors were detected</h3>'.$errors; }
•
•
Join Date: Apr 2008
Posts: 53
Reputation:
Solved Threads: 10
I think I may have spotted the problem
If you are not getting either of the outputs from above, it seems the function has been commented out. Try uncommenting the following line:
If you are not getting either of the outputs from above, it seems the function has been commented out. Try uncommenting the following line:
PHP Syntax (Toggle Plain Text)
//checkEnquiry();
•
•
Join Date: Apr 2008
Posts: 53
Reputation:
Solved Threads: 10
I think I've found why nothing is happening with the function.
It is probably being called on every page load, but the first thing it checks is This won't be true for 2 reasons:
1. the form contains the code: So submitting the form will send all the info to the email itself, not the server. Usually on a contact form, the server will handle the emailing with php's mail() function, so it is probably best to go down that avenue. Certainly to use PHP validations, you would have to submit to the server to begin with. To do this, just remove the action="" bit of code and the form will submit to the server
2. the form elements have not been named, so on each <input> you would have to add or as appropriate. The $_POST bit relies on a name being declared.
The next thing I would do (unless you can think of a reason why this shouldn't be the case), is run each of your validations in a series of if/elseif.
Also, please note I have spotted what I think is a final problem in your validations. They just need a ! in front to say if there is NO ereg of the following regular expression.
See below:
The advantage of this is that it will only run the validations if the content is not empty for each element. But unlike the previous code, it will still run validations on 'name' and 'email' even if 'questions' is absent
After this, if you choose to mail from the server, you may want to look into php's mail() function. I would recommend reading up on it on the PHP manual: http://www.php.net/manual/en/function.mail.php
A very basic way of doing this would be to add code like this to send the email:
I have quickly tested some of this code, but I haven't been able to test all of it. Also when I declared the $headers variable, I copied some old code that I used over a year ago when I was new to php, so there is a bad mix of single and double quotes. You may wish to try tidying it up, but I chose not to, because I wanted to copy code that I know worked previously.
You will obviously need to go through the code and check it does what you want it to and you will need to set it to automatically email to an address of your choice and set the subject as required.
Another thing I noticed, some errors are in <h3> tags, others in <p> tags, it would be best to standardise that.
If you need anything else, please let me know.
It is probably being called on every page load, but the first thing it checks is
PHP Syntax (Toggle Plain Text)
if (isset($_POST["submit"]))
1. the form contains the code:
PHP Syntax (Toggle Plain Text)
action="mailto:admin@******.net"
2. the form elements have not been named, so on each <input> you would have to add
PHP Syntax (Toggle Plain Text)
name='submit'
PHP Syntax (Toggle Plain Text)
name='name'
The next thing I would do (unless you can think of a reason why this shouldn't be the case), is run each of your validations in a series of if/elseif.
Also, please note I have spotted what I think is a final problem in your validations. They just need a ! in front to say if there is NO ereg of the following regular expression.
See below:
PHP Syntax (Toggle Plain Text)
// if the name is empty, create error if (empty($name)) { $errors .= '<p>Name field cannot be empty.</p>'; } // Make sure the name contains only letters elseif (!ereg('[^a-zA-Z]+', $name)) { $errors .= '<h3>Name can only contain letters.</h3>'; } // if the email is empty, create error if (empty($email)) { $errors .= '<p>Email field cannot be empty.</p>'; } //if email is set, check it is valid elseif (!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { $errors .= '<h3> Not a valid email address </h3>'; } // if the question is empty, create error if (empty($question)) { $errors .= '<p>Question field cannot be empty.</p>'; } // Make sure the question contains only alphanumeric and numbers elseif (!ereg('[^A-Za-z0-9]+', $question)) { $errors .= '<p>Your question can only contain letters and numbers.</p>'; }
After this, if you choose to mail from the server, you may want to look into php's mail() function. I would recommend reading up on it on the PHP manual: http://www.php.net/manual/en/function.mail.php
A very basic way of doing this would be to add code like this to send the email:
PHP Syntax (Toggle Plain Text)
if(empty($errors)) { $headers = 'Content-type: text/html; charset=iso-8859-1' . "\n"; $headers .= 'From: '.$name."<".$email.">"."\n"; mail( "example@domain.com", "Mail using contact form", "<html><body>".$question."</body></html>", $headers); echo '<h3>Your email was sent successfully</h3>'; } else { echo '<h3>Errors were detected</h3>'.$errors; }
I have quickly tested some of this code, but I haven't been able to test all of it. Also when I declared the $headers variable, I copied some old code that I used over a year ago when I was new to php, so there is a bad mix of single and double quotes. You may wish to try tidying it up, but I chose not to, because I wanted to copy code that I know worked previously.
You will obviously need to go through the code and check it does what you want it to and you will need to set it to automatically email to an address of your choice and set the subject as required.
Another thing I noticed, some errors are in <h3> tags, others in <p> tags, it would be best to standardise that.
If you need anything else, please let me know.
Thank you very much Simon, I have implementent all the changes you have suggested, I am not sure if I have done them correctly as this still doesn't work for me, you can see the results on the website link I sent you, it has been a very long night. I am a little confused what do i put in the form action=?
PHP Syntax (Toggle Plain Text)
<?php session_start(); // relevant variables for the header $page_title = "School Friends Club - Contact Us"; $page_meta_description = "School Friends Club contact us"; $page_meta_revised = "xxxxxxxx - 16/08/2009"; $page_name = "pageContact"; require ('includes/checkEnquiry.php'); checkEnquiry(); require ('includes/navigation.php'); ?> <!-- Content Div --> <div id="content"> <p><img src="images/contact title.jpg" alt="Contact Us" width="720" height="60" /> <img src="images/fancy67.gif" alt="bar" width="720" height="6" /></p> <!-- Contact Div - Left-side Pane --> <div id="contact"> <h3> The School Friends Club</h3> <p> Lourdes College, Traralgon Campus<br /> Building 12A, Level 2, Room 7<br /> 345 - 399 Grey Street <br /> PO Box 783 <br /> Traralgon VIC 3844 Australia <br /> Phone: +61 7 4651 5555<br /> Fax: +61 7 4651 7777<br /> Email: <a href="mailto:admin@sfc.com.au">admin@sfc.com.au</a></p> </div> <!-- close contact div--> <!-- Enquiry Div - Right-side Pane --> <?php require ('includes/enquiryform.php'); ?> </div> <!-- close content div--> <?php require ('includes/footer.php'); ?> </div> <!-- close page div --> </div> <!-- close container div --> </body> </html>
PHP Syntax (Toggle Plain Text)
<div id="enquiry"> <form action="" method="post"> <fieldset class="enquiryForm"> <legend class="legendHeader">Enquiry Form</legend> <br /> <label for="name">Name:</label> <input type="text" id="name" name="name"></input><br /> <label for="email">Email:</label> <input type="text" id="email" name="email"></input><br /> <label for="message">Message:</label> <textarea rows="3" cols="20" id="message" name="message"></textarea> <br /><br /> <input type="submit" name="submit" value="Submit"></input> <input type="reset" name="submit" value="Reset"></input> </fieldset> </form> </div> <!-- close enquiry div-->
PHP Syntax (Toggle Plain Text)
<?php // variable for any errors $errors; function checkEnquiry() { // if a user has just clicked submit, these will be set if (isset($_POST["submit"])) { // create variables to hold the data /*$to = "admin@xxxxxx.net"; $subject = "SFC Enquiry";*/ $name = $_POST['name'] ; $email = $_POST['email'] ; $message = $_POST['message'] ; /*$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message"; echo "Data has been submitted to $to!"; mail($to, $subject, $body);*/ // if the all fields contain data if (!empty($name)&& !empty($email)&& !empty($message)) { // if the name is empty, create error if (empty($name)) { $errors .= '<h3>Name field cannot be empty.</h3>'; } // Make sure the name contains only letters elseif (!ereg('[^a-zA-Z]+', $name)) { $errors .= '<h3>Name can only contain letters.</h3>'; } // if the email is empty, create error if (empty($email)) { $errors .= '<h3>Email field cannot be empty.</h3>'; } //if email is set, check it is valid elseif (!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { $errors .= '<h3> Not a valid email address </h3>'; } // if the question is empty, create error if (empty($message)) { $errors .= '<h3>Message field cannot be empty.</h3>'; } // Make sure the message contains only alphanumeric and numbers elseif (!ereg('[^A-Za-z0-9]+', $message)) { $errors .= '<h3>Your message can only contain letters and numbers.</h3>'; } if(empty($errors)) { $headers = 'Content-type: text/html; charset=iso-8859-1' . "\n"; $headers .= 'From: '.$name."<".$email.">"."\n"; mail( "admin@xxxxx.net", "A Message for SFC", "<html><body>".$message."</body></html>", $headers); echo '<h3>Your email was sent successfully</h3>'; } else { echo '<h3>Errors were detected</h3>'.$errors; } } } } ?>
Last edited by giddyupgirl; Aug 14th, 2009 at 8:09 pm.
•
•
Join Date: Apr 2008
Posts: 53
Reputation:
Solved Threads: 10
The first problem I can see with this is that you have a line of code that says
This would need to be removed, as the bit contained within that code now also validates for if the values are empty. So taking that if statement out will allow all of the validations to take place.
The action="" bit can just be removed altogether. It's not necessary when the page is submitting to itself, but I suppose it would be equally valid to say
Once you do this, you will probably find that two of your validations trigger, irrespective of what content is entered. The email validation works as expected, but I cannot seem to pass the 'name' or 'message' validations.
It's a while since I've worked with regular expressions, so I might not be able to help on that one. I'll keep looking, but you might be better off dabbling with the syntax you're using in ereg() for those two, until you get the desired effect.
PHP Syntax (Toggle Plain Text)
// if the all fields contain data if (!empty($name)&& !empty($email)&& !empty($message)) {
This would need to be removed, as the bit contained within that code now also validates for if the values are empty. So taking that if statement out will allow all of the validations to take place.
The action="" bit can just be removed altogether. It's not necessary when the page is submitting to itself, but I suppose it would be equally valid to say
PHP Syntax (Toggle Plain Text)
action="contact.php"
Once you do this, you will probably find that two of your validations trigger, irrespective of what content is entered. The email validation works as expected, but I cannot seem to pass the 'name' or 'message' validations.
It's a while since I've worked with regular expressions, so I might not be able to help on that one. I'll keep looking, but you might be better off dabbling with the syntax you're using in ereg() for those two, until you get the desired effect.
![]() |
Similar Threads
- Email form doesn't send everything (PHP)
- Edit email form replies to not show blank answers (HTML and CSS)
- Is it possible to create an "Email a Friend" or and email form... (ASP)
- email form problem (Web Hosting Deals)
- Please help with email form script (PHP)
- dreamweaver, PHP and submit form query (PHP)
- Need help with this email form (Java)
Other Threads in the PHP Forum
- Previous Thread: RSS feed reader
- Next Thread: Need help with uploading.....
Views: 546 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cookies cron curl database date directory display download dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail mediawiki menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql stored structure subdomain syntax system table tutorial update updates upload url validation validator variable video web xml youtube





