Help with Implementing email form validations please

Thread Solved

Join Date: May 2008
Posts: 7
Reputation: giddyupgirl is an unknown quantity at this point 
Solved Threads: 0
giddyupgirl's Avatar
giddyupgirl giddyupgirl is offline Offline
Newbie Poster

Help with Implementing email form validations please

 
0
  #1
Aug 14th, 2009
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
  1. <?php
  2. //session_start();
  3.  
  4. // relevant variables for the header
  5. $page_title = "School Friends Club - Contact Us";
  6. $page_meta_description = "School Friends Club contact us";
  7. $page_meta_revised = "******* - 16/08/2009";
  8. $page_name = "pageContact";
  9.  
  10. require ('includes/checkEnquiry.php');
  11.  
  12. //checkEnquiry();
  13.  
  14. require ('includes/navigation.php');
  15. ?>
  16.  
  17. <!-- Content Div -->
  18. <div id="content">
  19.  
  20. <p><img src="images/contact title.jpg" alt="Contact Us" width="720" height="60" />
  21. <img src="images/fancy67.gif" alt="bar" width="720" height="6" /></p>
  22.  
  23.  
  24. <!-- Contact Div - Left-side Pane -->
  25. <div id="contact">
  26.  
  27. <h3> The School Friends Club</h3>
  28.  
  29. <p> Lourdes College, Traralgon Campus<br />
  30. Building 12A, Level 2, Room 7<br />
  31. 345 - 399 Grey Street <br />
  32. PO Box 783 <br />
  33. Traralgon VIC 3844 Australia <br />
  34. Phone: +61 7 4651 5555<br />
  35. Fax: +61 7 4651 7777<br />
  36. Email: <a href="mailto:admin@sfc.com.au">admin@sfc.com.au</a></p>
  37.  
  38. </div> <!-- close contact div-->
  39.  
  40. <!-- Enquiry Div - Right-side Pane -->
  41.  
  42. <?php
  43. require ('includes/enquiryform.php');
  44. ?>
  45.  
  46. </div> <!-- close content div-->
  47.  
  48. <?php
  49. require ('includes/footer.php');
  50. ?>
  51.  
  52.  
  53. </div> <!-- close page div -->
  54.  
  55. </div> <!-- close container div -->
  56.  
  57. </body>
  58. </html>


includes/enquiryCheck.php

  1. <?php
  2.  
  3. // variable for any errors
  4. $errors;
  5. /*function checkEnquiry()*/
  6. {
  7.  
  8. // if a user has just clicked submit, these will be set
  9. if (isset($_POST["submit"]))
  10. {
  11.  
  12. // create variables to hold the data$name = $_POST['name'] ;
  13. $name = $_POST['name'] ;
  14. $email = $_POST['email'] ;
  15. $question = $_POST['question'] ;
  16.  
  17. // if the all fields contain data
  18. if (!empty($name)&& !empty($email)&& !empty($question))
  19. {
  20.  
  21. // Make sure the name contains only letters
  22.  
  23. if (ereg('[^a-zA-Z]+', $name))
  24. {
  25. $errors .= '<h3>Name can only contain letters.</h3>';
  26. }
  27.  
  28. if (ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
  29. {
  30. $errors .= '<h3> Not a valid email address </h3>';
  31.  
  32. }
  33. // Make sure the question contains only alphanumeric and numbers
  34.  
  35. if (ereg('[^A-Za-z0-9]+', $question))
  36. {
  37. $errors .= '<p>Your question can only contain letters and numbers.</p>';
  38. }
  39. }
  40.  
  41. else
  42. {
  43. // if the name is empty, create error
  44. if (empty($name))
  45. {
  46. $errors .= '<p>Name field cannot be empty.</p>';
  47. }
  48. // if the email is empty, create error
  49. if (empty($email))
  50. {
  51. $errors .= '<p>Email field cannot be empty.</p>';
  52. }
  53. // if the question is empty, create error
  54. if (empty($question))
  55. {
  56. $errors .= '<p>Question field cannot be empty.</p>';
  57. }
  58.  
  59. /*// if errors were created, print them to the screen
  60. if(empty($errors))
  61. {
  62. echo '<h3>Your email was sent successfully</h3>';
  63. }*/
  64. }
  65. }
  66. }
  67. ?>


includes/enquiryform.php

  1. <div id="enquiry">
  2. <form action="mailto:admin@******.net" method="post">
  3. <fieldset class="enquiryForm">
  4. <legend class="legendHeader">Enquiry Form</legend>
  5. <br />
  6. <label for="name">Name:</label>
  7. <input type="text" id="name"></input><br />
  8. <label for="email">Email:</label>
  9. <input type="text" id="email" ></input><br />
  10. <label for="question">Question:</label>
  11. <textarea rows="3" cols="20" id="question"></textarea>
  12. <br /><br />
  13. <input type="submit" value="Submit"></input>
  14. <input type="reset" value="Reset"></input>
  15. </fieldset>
  16. </form>
  17. </div> <!-- close enquiry div-->
Last edited by giddyupgirl; Aug 14th, 2009 at 6:56 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 53
Reputation: SimonMayer is an unknown quantity at this point 
Solved Threads: 10
SimonMayer SimonMayer is offline Offline
Junior Poster in Training

Re: Help with Implementing email form validations please

 
0
  #2
Aug 14th, 2009
Originally Posted by giddyupgirl View Post
  1. // if errors were created, print them to the screen
  2. if(empty($errors))
  3. {
  4. echo '<h3>Your email was sent successfully</h3>';
  5. }
Please could you provide some more details about exactly what is happening (or not happening)? i.e. is the problem just that it won't show the errors on screen, if so you just need to add to the bit I've quoted above
  1. else
  2. {
  3. echo '<h3>Errors were detected</h3>'.$errors;
  4. }

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
Regards,

Simon Mayer
Website design by Ribbontree
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 7
Reputation: giddyupgirl is an unknown quantity at this point 
Solved Threads: 0
giddyupgirl's Avatar
giddyupgirl giddyupgirl is offline Offline
Newbie Poster

Re: Help with Implementing email form validations please

 
0
  #3
Aug 14th, 2009
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.
Last edited by giddyupgirl; Aug 14th, 2009 at 7:50 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 53
Reputation: SimonMayer is an unknown quantity at this point 
Solved Threads: 10
SimonMayer SimonMayer is offline Offline
Junior Poster in Training

Re: Help with Implementing email form validations please

 
0
  #4
Aug 14th, 2009
The bit in your code that echoes whether or not the email was sent seems to be commented out. Have you removed the comments?
  1. /*// if errors were created, print them to the screen
  2. if(empty($errors))
  3. {
  4. echo '<h3>Your email was sent successfully</h3>';
  5. }*/

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.

  1. if(empty($errors))
  2. {
  3. echo '<h3>Your email was sent successfully</h3>';
  4. }
  5. else {
  6. echo '<h3>Errors were detected</h3>'.$errors;
  7. }
Regards,

Simon Mayer
Website design by Ribbontree
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 7
Reputation: giddyupgirl is an unknown quantity at this point 
Solved Threads: 0
giddyupgirl's Avatar
giddyupgirl giddyupgirl is offline Offline
Newbie Poster

Re: Help with Implementing email form validations please

 
0
  #5
Aug 14th, 2009
thank you Simon, yes I have done that but unfortunately it still doesn't work, I don't think the function is being called properly but I don't know how to resolve that problem.
cheers
giddyupgirl
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 53
Reputation: SimonMayer is an unknown quantity at this point 
Solved Threads: 10
SimonMayer SimonMayer is offline Offline
Junior Poster in Training

Re: Help with Implementing email form validations please

 
0
  #6
Aug 14th, 2009
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:
  1. //checkEnquiry();
Regards,

Simon Mayer
Website design by Ribbontree
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 7
Reputation: giddyupgirl is an unknown quantity at this point 
Solved Threads: 0
giddyupgirl's Avatar
giddyupgirl giddyupgirl is offline Offline
Newbie Poster

Re: Help with Implementing email form validations please

 
0
  #7
Aug 14th, 2009
Sorry still no luck, I have sent you a private message with the url of where the actual website is locate if that is of any use.
Thank you for your patience.
cheers
giddyupgirl
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 53
Reputation: SimonMayer is an unknown quantity at this point 
Solved Threads: 10
SimonMayer SimonMayer is offline Offline
Junior Poster in Training

Re: Help with Implementing email form validations please

 
0
  #8
Aug 14th, 2009
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
  1. if (isset($_POST["submit"]))
This won't be true for 2 reasons:
1. the form contains the code:
  1. action="mailto:admin@******.net"
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
  1. name='submit'
or
  1. name='name'
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:
  1. // if the name is empty, create error
  2. if (empty($name))
  3. {
  4. $errors .= '<p>Name field cannot be empty.</p>';
  5. }
  6. // Make sure the name contains only letters
  7. elseif (!ereg('[^a-zA-Z]+', $name))
  8. {
  9. $errors .= '<h3>Name can only contain letters.</h3>';
  10. }
  11.  
  12. // if the email is empty, create error
  13. if (empty($email))
  14. {
  15. $errors .= '<p>Email field cannot be empty.</p>';
  16. }
  17. //if email is set, check it is valid
  18. elseif (!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
  19. {
  20. $errors .= '<h3> Not a valid email address </h3>';
  21. }
  22.  
  23. // if the question is empty, create error
  24. if (empty($question))
  25. {
  26. $errors .= '<p>Question field cannot be empty.</p>';
  27. }
  28. // Make sure the question contains only alphanumeric and numbers
  29. elseif (!ereg('[^A-Za-z0-9]+', $question))
  30. {
  31. $errors .= '<p>Your question can only contain letters and numbers.</p>';
  32. }
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:
  1. if(empty($errors))
  2. {
  3. $headers = 'Content-type: text/html; charset=iso-8859-1' . "\n";
  4. $headers .= 'From: '.$name."<".$email.">"."\n";
  5. mail(
  6. "example@domain.com",
  7. "Mail using contact form",
  8. "<html><body>".$question."</body></html>",
  9. $headers);
  10. echo '<h3>Your email was sent successfully</h3>';
  11. }
  12. else
  13. {
  14. echo '<h3>Errors were detected</h3>'.$errors;
  15. }

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.
Regards,

Simon Mayer
Website design by Ribbontree
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 7
Reputation: giddyupgirl is an unknown quantity at this point 
Solved Threads: 0
giddyupgirl's Avatar
giddyupgirl giddyupgirl is offline Offline
Newbie Poster

Re: Help with Implementing email form validations please

 
0
  #9
Aug 14th, 2009
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=?

  1. <?php
  2. session_start();
  3.  
  4. // relevant variables for the header
  5. $page_title = "School Friends Club - Contact Us";
  6. $page_meta_description = "School Friends Club contact us";
  7. $page_meta_revised = "xxxxxxxx - 16/08/2009";
  8. $page_name = "pageContact";
  9.  
  10. require ('includes/checkEnquiry.php');
  11.  
  12. checkEnquiry();
  13.  
  14. require ('includes/navigation.php');
  15. ?>
  16.  
  17. <!-- Content Div -->
  18. <div id="content">
  19.  
  20. <p><img src="images/contact title.jpg" alt="Contact Us" width="720" height="60" />
  21. <img src="images/fancy67.gif" alt="bar" width="720" height="6" /></p>
  22.  
  23.  
  24. <!-- Contact Div - Left-side Pane -->
  25. <div id="contact">
  26.  
  27. <h3> The School Friends Club</h3>
  28.  
  29. <p> Lourdes College, Traralgon Campus<br />
  30. Building 12A, Level 2, Room 7<br />
  31. 345 - 399 Grey Street <br />
  32. PO Box 783 <br />
  33. Traralgon VIC 3844 Australia <br />
  34. Phone: +61 7 4651 5555<br />
  35. Fax: +61 7 4651 7777<br />
  36. Email: <a href="mailto:admin@sfc.com.au">admin@sfc.com.au</a></p>
  37.  
  38. </div> <!-- close contact div-->
  39.  
  40. <!-- Enquiry Div - Right-side Pane -->
  41.  
  42. <?php
  43. require ('includes/enquiryform.php');
  44. ?>
  45.  
  46. </div> <!-- close content div-->
  47.  
  48. <?php
  49. require ('includes/footer.php');
  50. ?>
  51.  
  52.  
  53. </div> <!-- close page div -->
  54.  
  55. </div> <!-- close container div -->
  56.  
  57. </body>
  58. </html>

  1. <div id="enquiry">
  2. <form action="" method="post">
  3. <fieldset class="enquiryForm">
  4. <legend class="legendHeader">Enquiry Form</legend>
  5. <br />
  6. <label for="name">Name:</label>
  7. <input type="text" id="name" name="name"></input><br />
  8. <label for="email">Email:</label>
  9. <input type="text" id="email" name="email"></input><br />
  10. <label for="message">Message:</label>
  11. <textarea rows="3" cols="20" id="message" name="message"></textarea>
  12. <br /><br />
  13. <input type="submit" name="submit" value="Submit"></input>
  14. <input type="reset" name="submit" value="Reset"></input>
  15. </fieldset>
  16. </form>
  17. </div> <!-- close enquiry div-->

  1. <?php
  2.  
  3. // variable for any errors
  4. $errors;
  5. function checkEnquiry()
  6.  
  7. {
  8. // if a user has just clicked submit, these will be set
  9. if (isset($_POST["submit"]))
  10. {
  11.  
  12. // create variables to hold the data
  13. /*$to = "admin@xxxxxx.net";
  14. $subject = "SFC Enquiry";*/
  15. $name = $_POST['name'] ;
  16. $email = $_POST['email'] ;
  17. $message = $_POST['message'] ;
  18.  
  19. /*$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
  20.  
  21. echo "Data has been submitted to $to!";
  22. mail($to, $subject, $body);*/
  23.  
  24.  
  25. // if the all fields contain data
  26. if (!empty($name)&& !empty($email)&& !empty($message))
  27. {
  28.  
  29. // if the name is empty, create error
  30. if (empty($name))
  31. {
  32. $errors .= '<h3>Name field cannot be empty.</h3>';
  33. }
  34. // Make sure the name contains only letters
  35. elseif (!ereg('[^a-zA-Z]+', $name))
  36. {
  37. $errors .= '<h3>Name can only contain letters.</h3>';
  38. }
  39.  
  40. // if the email is empty, create error
  41. if (empty($email))
  42. {
  43. $errors .= '<h3>Email field cannot be empty.</h3>';
  44. }
  45. //if email is set, check it is valid
  46. elseif (!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
  47. {
  48. $errors .= '<h3> Not a valid email address </h3>';
  49. }
  50.  
  51. // if the question is empty, create error
  52. if (empty($message))
  53. {
  54. $errors .= '<h3>Message field cannot be empty.</h3>';
  55. }
  56. // Make sure the message contains only alphanumeric and numbers
  57. elseif (!ereg('[^A-Za-z0-9]+', $message))
  58. {
  59. $errors .= '<h3>Your message can only contain letters and numbers.</h3>';
  60. }
  61. if(empty($errors))
  62. {
  63. $headers = 'Content-type: text/html; charset=iso-8859-1' . "\n";
  64. $headers .= 'From: '.$name."<".$email.">"."\n";
  65. mail(
  66. "admin@xxxxx.net",
  67. "A Message for SFC",
  68. "<html><body>".$message."</body></html>",
  69. $headers);
  70. echo '<h3>Your email was sent successfully</h3>';
  71. }
  72. else
  73. {
  74. echo '<h3>Errors were detected</h3>'.$errors;
  75. }
  76.  
  77. }
  78. }
  79. }
  80. ?>
Last edited by giddyupgirl; Aug 14th, 2009 at 8:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 53
Reputation: SimonMayer is an unknown quantity at this point 
Solved Threads: 10
SimonMayer SimonMayer is offline Offline
Junior Poster in Training

Re: Help with Implementing email form validations please

 
0
  #10
Aug 15th, 2009
The first problem I can see with this is that you have a line of code that says
  1. // if the all fields contain data
  2. if (!empty($name)&& !empty($email)&& !empty($message))
  3. {

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
  1. 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.
Regards,

Simon Mayer
Website design by Ribbontree
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 546 | Replies: 13
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC