PHP Form Validation ???

Reply

Join Date: Jan 2006
Posts: 89
Reputation: j4mes_bond25 is an unknown quantity at this point 
Solved Threads: 0
j4mes_bond25's Avatar
j4mes_bond25 j4mes_bond25 is offline Offline
Junior Poster in Training

PHP Form Validation ???

 
0
  #1
May 25th, 2006
I wonder if anyone around could possibly enlighten me a little (or indeed, a LOT) towards "Contact Us" form validation in PHP.

Basically, I've created the form & then typed in the validation related codes from 3 different books along with differerent websites so far, but ONLY, if I understood it.

However, there are only couple things that's beyond me, which can be seen from the comment around the code that I didn't understand i.e. comment typed after "//"

Much of the code is still missing bearing in mind my expectation from this form and my lack of knowledge in how to go about doing it.

This is what I'm aiming for:

>> Users MUST fill in all the fields (including radio button field and drop-down menu field)
>> If they don't then they should get a message saying so
>> If they don't fill any certain field then they should get a message saying "Please fill in your E-mail" (for example) OR if they don't select appropriate "radio button" option or "drop-down" menu option then they should get an error message saying "please select the appropriate "Title" from the drop-down list", etc. OR "please choose the means of contacting you" (by choosing appropriate radio button)
>> The "blanked" or "incorrect data entry" field should turn to "red" making it easier for user to realise what's missing

>> After user fills everything & when they click "Preview" they should get some sort of "message" (ONLY if the data is valid and ALL required fields filled) saying "Following are the details you filled in" and then underneath it, CONFIRM their title, name, e-mail, telephone number, message, ways of contacting back to them, etc. (basically, ALL the field) with "Submit" and "Edit" button. Selecting "Edit" will take them back to the form, with all the data they filled in STILL there (rather than making the re-type everything)
>> If they simply click on submit (after filling the required & valid data) then they should get a message saying "Thanks for your message, we'll be in touch shortly".

The codes that I've created as yet are as follows:

  1. <?php include("menu.inc");?>
  2.  
  3. <div id="centerContent">
  4.  
  5. <p class="first-letter">Please fill the following form in for any enquiries that you may have:</p>
  6. <p>
  7.  
  8. <?php
  9.  
  10. // if the form has been posted, analyse it:
  11. if ($_POST) {
  12. foreach ($_POST as $field => $value) {
  13. $value = trim($value);
  14. }
  15.  
  16. // creating variables
  17. $inquiry=$_POST['inquiry'];
  18. $title=$_POST['title'];
  19. $first_name=$_POST['first_name'];
  20. $last_name=$_POST['last_name'];
  21. $email=$_POST['email'];
  22. $phone=$_POST['phone'];
  23. $message=$_POST['message'];
  24. $reply=$_POST['reply'];
  25. $contact=$_POST['contact'];
  26.  
  27. // create empty error variable
  28. $error = "";
  29.  
  30. // check for data in required fields
  31. if (($inquiry == "") || ($title == "") || ($first_name == "") || ($last_name == "") || ($email == "") || ($phone == "") || ($message == "") || ($reply == "") || ($contact == "")) {
  32. $error = "Please fill in all the required fields!";
  33. }
  34.  
  35. // validate first_name
  36. if ((ctype_alpha($first_name) == FALSE
  37. $error = "Please enter a valid name (Alphabets only)<br/>";
  38. }
  39.  
  40. // validate surname
  41. if ((ctype_alpha($last_name) == FALSE {
  42. $error = "Please enter a valid last name (Alphabets only)<br/>";
  43. }
  44.  
  45. // validate e-mail
  46. if ((strpos($email, "@") === FALSE ||
  47. (strpos($email, ".") === FALSE ||
  48. (strpos($email, " ") != FALSE || // DON'T KNOW WHY USING "!=" SIGN
  49. (strpos($email, "@") === FALSE || // DON'T KNOW WHY USING 3 "=" SIGN
  50. (strpos($email, "@") > strrpos($email, "."))) {
  51. $error = "Please enter a valid e-mail address<br/>";
  52. }
  53.  
  54. // validate phone
  55. if (is_numeric($phone) == FALSE {
  56. $error = "Please enter a valid contact number (must contain numbers only)<br/>";
  57. }
  58.  
  59. //If everything is OK then Confirmation of each field's data as filled by the user
  60. else {
  61. echo "<p><b>Nature of Inquiry:</b> $inquiry<br/>";
  62. echo "<p><b>Title:</b> $title<br/>";
  63. echo "<p><b>First Name:</b> $first_name<br/>";
  64. echo "<p><b>Last Name:</b> $last_name<br/>";
  65. echo "<p><b>E-mail:</b> $email<br/>";
  66. echo "<p><b>Contact No.:</b> $phone<br/>";
  67. echo "<p><b>Message:</b> $message<br/>";
  68. echo "<p><b>Reply:</b> $reply<br/>";
  69. echo "<p><b>Contact Method:</b> $contact<br/>";
  70.  
  71. echo "Thanks for contacting us. We will get back to you shortly!";
  72. }
  73. }
  74. ?>
  75. </p>
  76.  
  77. <table class="table">
  78.  
  79. <tr class="tr">
  80. <td class="td"><form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
  81. Nature of Enquiry:</td>
  82. <td class="td"><select name="inquiry" value="<?php echo $inquiry ?>">
  83. <option></option>
  84. <option>General Inquiry</option>
  85. <option>Price Quotation</option>
  86. <option>Comments</option>
  87. <option>Other</option>
  88. </select></td>
  89. </tr>
  90.  
  91. <tr class="tr">
  92. <td class="td">Title:</td>
  93. <td class="td"><select name="title" value="<?php echo $title ?>">
  94. <option></option>
  95. <option>Mr</option>
  96. <option>Miss</option>
  97. <option>Mrs</option>
  98. <option>Other</option>
  99. </select></td>
  100. </tr>
  101.  
  102. <tr class="tr">
  103. <td class="td">First Name:</td>
  104. <td class="td"><input type="text" size="30" maxlength="30" name="first_name" value="<?php echo $first_name ?>"/></td>
  105. </tr>
  106.  
  107. <tr class="tr">
  108. <td class="td">Last Name:</td>
  109. <td class="td"><input type="text" size="30" maxlength="30" name="last_name" value="<?php echo $last_name ?>"/></td>
  110. </tr>
  111.  
  112. <tr class="tr">
  113. <td class="td">E-mail:</td>
  114. <td class="td"><input type="text" size="30" maxlength="30" name="email" value="<?php echo $email ?>"/></td>
  115. </tr>
  116.  
  117. <tr class="tr">
  118. <td class="td">Contact No.:</td>
  119. <td class="td"><input type="text" size="30" maxlength="20" name="phone" value="<?php echo $phone ?>"/></td>
  120. </tr>
  121.  
  122. <tr class="tr">
  123. <td class="td">Message:</td>
  124. <td class="td"><textarea rows="10" cols="50" wrap="physical" name="message" value="<?php echo $message ?>">
  125. </textarea></td>
  126. </tr>
  127.  
  128. <tr class="tr">
  129. <td class="td">Reply Required:</td>
  130. <td class="td"><input type="radio" name="reply" value="<?php echo $reply ?>"/>Yes
  131. <input name="reply" type="radio"/>No
  132. <input name="reply" type="radio"/>Maybe</td>
  133. </tr>
  134.  
  135. <tr class="tr">
  136. <td class="td">How would you like to be contacted (if required)?<br/><br/></td>
  137. <td class="td"><input type="radio" name="contact" value="<?php echo $contact ?>"/>E-mail
  138. <input type="radio" name="contact"/>Telephone</td>
  139. </tr>
  140.  
  141. <tr class="tr">
  142. <td class="td"></td>
  143. <td class="td"><input type="reset" name="reset" value="Reset"/>
  144. <input type="preview" name="preview" value="Preview"/></td>
  145. <input type="submit" name="submit" value="Submit"/></td>
  146. </form>
  147. </tr>
  148. </table>
  149. </div>
Nope, I'm NOT God, but I'm British (which is the next best thing ;)
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3
Reputation: navvy is an unknown quantity at this point 
Solved Threads: 0
navvy navvy is offline Offline
Newbie Poster

hello j4mes

 
0
  #2
May 26th, 2006
send me the include files also eg(menu.inc) i can help u on this validation
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 89
Reputation: j4mes_bond25 is an unknown quantity at this point 
Solved Threads: 0
j4mes_bond25's Avatar
j4mes_bond25 j4mes_bond25 is offline Offline
Junior Poster in Training

Re: hello j4mes

 
0
  #3
May 26th, 2006
Originally Posted by navvy
send me the include files also eg(menu.inc) i can help u on this validation
I sent you everything through the private message by replying back to your message.
Nope, I'm NOT God, but I'm British (which is the next best thing ;)
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: hello j4mes

 
0
  #4
May 26th, 2006
(strpos($email, " ") != FALSE || // DON'T KNOW WHY USING "!=" SIGN
(strpos($email, "@") === FALSE || // DON'T KNOW WHY USING 3 "=" SIGN
[php](strpos($email, " ") != FALSE[/php]
is the same as
[php](strpos($email, " ") == TRUE[/php]

!= (NOT EQUALS) is the opposite of == (EQUALS).

strpos($email, " ") checks for the postiion of " ", (a space) in the string, $email. It will return the position of the string if found, or FALSE if it does not exist.
In this context, we are searching for a space in the email address, if a space is found (!= FALSE means it doesnt return false) then the email address must be invalid as emails dont allow spaces.


=== means something like EQUAL to and EQUIVALENT TO.

== only tests the numerical values, === will test for the numerical value as well as the type.

So === means equal to and of the same type.

its used in strpos($email, "@") === FALSE because strpos() can return 0 if the @ is at the beginning of the string.
So === makes sure that if strpos($email, "@") returns 0, then 0 === FALSE will be false, since 0 is not the same type as FALSE, even though they are equal (==)numerically.

However, I dont think having @ as the first character in an email is valid, so you should ditch the === for ==. So that it also tests that @ is in the middle of the string, and not at the first position.

Resources:
http://www.php.net/operators.comparison
http://www.php.net/manual/en/types.comparisons.php

I'd also suggest you look at:
http://www.deformedweb.co.uk/php_variable_tests.php

And also at type juggling, as automatic type conversions affect comparisons.
http://www.php.net/manual/en/languag...e-juggling.php
http://www.php.net/manual/en/language.types.string.php

HOpe that helps with that bit...
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 4
Reputation: hookedonphp is an unknown quantity at this point 
Solved Threads: 0
hookedonphp hookedonphp is offline Offline
Newbie Poster

Re: PHP Form Validation ???

 
0
  #5
Nov 13th, 2006
Create your MySQL table first, to receive the submitted data. Then write a script to analyze the table and create your forms.

Select a table from
http://www.php-form-generator.com/php-script/
and click links on the next page to create different forms.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 53
Reputation: Barnz is an unknown quantity at this point 
Solved Threads: 0
Barnz Barnz is offline Offline
Junior Poster in Training

Re: PHP Form Validation ???

 
0
  #6
Nov 13th, 2006
EDIT: My mistake

Got the strpos muddled up with strstr.
Last edited by Barnz; Nov 13th, 2006 at 5:38 pm.
www.jab.poundingbeats.com - My PHP website.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: krishikrishi1 is an unknown quantity at this point 
Solved Threads: 0
krishikrishi1 krishikrishi1 is offline Offline
Newbie Poster

hi this is ram

 
0
  #7
Nov 5th, 2008
i already submited for asking about menu.inc where is that file it consists of entire functionality i missing it please let me send that file please <EMAIL SNIPPED>


i iwll waiting for your replay please


Thanks & Regards
Last edited by peter_budo; Nov 5th, 2008 at 9:03 pm. Reason: Keep It On The Site - Do not post asking for an answer to be sent to you via email or PM.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC