Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
11/20/2009 1:05:23 PM EDT
My wife is having trouble with PHP, thought i'd check here for her to see if anyone could help.  Here's what she posted on another forum.  



Hi, I'm new to PHP and I really don' know where to even start. I have to validate a form on my website. The form contains many fields and all fields are required and I should have an error message that will appear if they are not complete or completed incorrectly. I also have to make sure that the phone number is ###-###-#### format and the email is [email protected] format.

When the form is successfully completed there should be a confirmation page with the information on it and a thank you message. The entered info should be written to a text file and emails should be sent to myself and the visitor. The visitor's email should be a confirmation email requiring a reply.

I've been searching for help but I don't even understand the basic code.

I found something I thought may work so I tried it but I was not successful with it. Here's the code I tried to use. It was only checking the fields to make sure they were not empty...but it still allowed the form to be processed with empty fields.


  1. <?
  2. If (empty($_POST['fname']))
  3. {
  4. $errors[] = 'Please enter a first name';
  5. }
  6.  
  7.  
  8. If (empty($_POST['lname']))
  9. {
 10. $errors[] = 'Please enter a last name';
 11. }
 12.  
 13. If (empty($_POST['title']))
 14. {
 15. $errors[] = 'Please enter a job title';
 16. }
 17.  
 18. If (empty($_POST['company']))
 19. {
 20. $errors[] = 'Please enter your company name';
 21. }
 22.  
 23. If (empty($_POST['address']))
 24. {
 25. $errors[] = 'Please enter your mailing address';
 26. }
 27.  
 28. If (empty($_POST['city']))
 29. {
 30. $errors[] = 'Please enter your city';
 31. }
 32.  
 33. If (empty($_POST['zip']))
 34. {
 35. $errors[] = 'Please enter your zip code';
 36. }
 37.  
 38. If (empty($_POST['state']))
 39. {
 40. $errors[] = 'Please enter your state';
 41. }
 42.  
 43. If (empty($_POST['phone']))
 44. {
 45. $errors[] = 'Please enter your phone number';
 46. }
 47.  
 48.  
 49. If (empty($_POST['fax']))
 50. {
 51. $errors[] = 'Please enter a fax number';
 52. }
 53.  
 54. If (empty($_POST['email']))
 55. {
 56. $errors[] = 'Please enter your email address';
 57. }
 58.  
 59. else if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $_POST['email']))
 60. {
 61. $errors[] = 'Please enter a valid e-mail address';
 62. }
 63.  
 64. if (count($errors) == 0)
 65. {
 66. // Process form
 67. }
 68. else
 69. {
 70. echo $errors[0];
 71. }
 72. ?>
 73.  
11/20/2009 3:05:18 PM EDT
[#1]
i'm not a php guy.. more a mainframe assembler.. kiddin', but she is not describing her errors beyond 'it broke'





could she be more specific?





edit:



libertynews might know.. from queendenile's thread.

11/20/2009 3:13:36 PM EDT
[#2]
Ensure the form method is POST, ensure the name of each input element is the same as the associative index you're using in the $_POST array.

If $errors is empty, post the confirmation page.

Otherwise, display the form again along with the errors.

It should be pretty straightforward.
11/20/2009 7:07:14 PM EDT
[#3]
The code that she has, that I just posted, still allows the user to submit a form although the form is not completed. She doesn't know PHP at all. She's trying to use code snippets to make something work good enough for her project. Without understanding PHP she's having a hard time figuring out what needs to be done.
11/20/2009 9:45:49 PM EDT
[#4]
Try this.

Just whipped it up, didn't try it, should work though.


<?php

$requiredFields = array(
 'fname',
 'lname',
 'title',
 'company',
 'address',
 'city',
 'zip',
 'state',
 'phone',
 'fax',
 'email',
)

$errorStrings = array(
 'fname' => 'Please enter a first name',
 'lname' => 'Please enter a last name',
 'title' => 'Please enter a job title',
 'company' => 'Please enter your company name',
 'address' => 'Please enter your mailing address',
 'city' => 'Please enter your city',
 'zip' => 'Please enter your zip code',
 'state' => 'Please enter your state',,
 'phone' => 'Please enter your phone number',
 'fax' => 'Please enter a fax number',
 'email' => 'Please enter your email address',
);

$errors = array();

foreach ($requiredFields as $field)
{
 // $_REQUEST checks GET, POST, COOKIES and the environment.
 if (isset($_REQUEST[$field]))
 {
   // This will make sure they aren't submitted as all blank/spaces.
   if (!trim($_REQUEST[$field]))
   {
     $errors[] = $errorStrings[$field];
   }
 }
 else
 {
   $errors[] = $errorStrings[$field];
 }
}

if ($errors)
{
 print_r($errors);
}
else
{
 // do your eregi check on $_REQUEST['email'] here...
 if (regex)
 {
   // process form
 }
 else
 {
   print('Enter a valid email address.')
 }
}

?>


ETA: formatting, dunno why 'code' block is doublespacing.
11/21/2009 1:26:09 AM EDT
[#5]
The form will be submitted without first verifying the contents of the input fields unless she uses JavaScript. JavaScript is executed client side and can prevent submission of the form if the fields do not meet her requirements. PHP is only executed once a request has been sent to the HTTP server (in this case, once the form has been submitted).

It'd probably be useful for her in this case to verify the client input on the client side (with JavaScript) and then again on the server side (with PHP).
11/21/2009 7:11:33 PM EDT
[#6]
Quoted:
The form will be submitted without first verifying the contents of the input fields unless she uses JavaScript. JavaScript is executed client side and can prevent submission of the form if the fields do not meet her requirements. PHP is only executed once a request has been sent to the HTTP server (in this case, once the form has been submitted).

It'd probably be useful for her in this case to verify the client input on the client side (with JavaScript) and then again on the server side (with PHP).


That's typically what I do these days. 2 layers of checks, one in JS to give nice immediate feedback to the user and another check in PHP to make sure nothing insane is being passed. On the JS side I use the jQuery framework –– makes things alot easier.

That being said, if she doesn't know PHP I would recommend learning it first before trying to paste some code together –– the world is already full of badly written PHP scripts and it doesn't need anymore.

There are numerous books out there for beginning PHP programmers, I tend to like books from O'Reilly and from APress.

11/21/2009 11:57:10 PM EDT
[#7]
Quoted:
Quoted:
The form will be submitted without first verifying the contents of the input fields unless she uses JavaScript. JavaScript is executed client side and can prevent submission of the form if the fields do not meet her requirements. PHP is only executed once a request has been sent to the HTTP server (in this case, once the form has been submitted).

It'd probably be useful for her in this case to verify the client input on the client side (with JavaScript) and then again on the server side (with PHP).


That's typically what I do these days. 2 layers of checks, one in JS to give nice immediate feedback to the user and another check in PHP to make sure nothing insane is being passed. On the JS side I use the jQuery framework –– makes things alot easier.

That being said, if she doesn't know PHP I would recommend learning it first before trying to paste some code together –– the world is already full of badly written PHP scripts and it doesn't need anymore.

There are numerous books out there for beginning PHP programmers, I tend to like books from O'Reilly and from APress.



jQuery is great.  I use it as well, and for form validation, doing it with ajax gets you bonus points for calling the same validation lib in both cases.