Error in php form validation

Reply

Join Date: Dec 2008
Posts: 7
Reputation: rajesh.bl is an unknown quantity at this point 
Solved Threads: 0
rajesh.bl rajesh.bl is offline Offline
Newbie Poster

Error in php form validation

 
0
  #1
Dec 18th, 2008
Hi All,

I am using the following code for form validation but i am getting an error saying that unexpected T_variable in line

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

Please help me on this......

Waiting for the reply...
Last edited by peter_budo; Dec 18th, 2008 at 3:24 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 94
Reputation: sikka_varun is an unknown quantity at this point 
Solved Threads: 11
sikka_varun's Avatar
sikka_varun sikka_varun is offline Offline
Junior Poster in Training

Re: Error in php form validation

 
0
  #2
Dec 18th, 2008
Hi..
In validating phone you missed out one bracket { in the if condition

  1. // validate phone
  2. if (is_numeric($phone) == FALSE [COLOR="Red"])[/COLOR] {
  3. $error = "Please enter a valid contact number (must contain numbers only)<br/>";
  4. }
VâRûN
---Happy to Help---
sikka_varun@yahoo.com
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 94
Reputation: sikka_varun is an unknown quantity at this point 
Solved Threads: 11
sikka_varun's Avatar
sikka_varun sikka_varun is offline Offline
Junior Poster in Training

Re: Error in php form validation

 
0
  #3
Dec 18th, 2008
Oops i was trying to highlight the ) bracket (not curly one { )
I'll paste code here again..

// validate phone
if (is_numeric($phone) == FALSE ) {
$error = "Please enter a valid contact number (must contain numbers only)<br/>";
}
VâRûN
---Happy to Help---
sikka_varun@yahoo.com
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 7
Reputation: rajesh.bl is an unknown quantity at this point 
Solved Threads: 0
rajesh.bl rajesh.bl is offline Offline
Newbie Poster

Re: Error in php form validation

 
0
  #4
Dec 18th, 2008
Thanx a lot for ur reply i have tried with that and the error is solved but i am getting one more error

syntax error, unexpected '{' on line

i,e in this code
  1. // validate e-mail
  2. if ((strpos($email, "@") === FALSE ||
  3. (strpos($email, ".") === FALSE ||
  4. (strpos($email, " ") != FALSE || // DON'T KNOW WHY USING "!=" SIGN
  5. (strpos($email, "@") === FALSE || // DON'T KNOW WHY USING 3 "=" SIGN
  6. (strpos($email, "@") > strrpos($email, ".")))
  7. {
  8. $error = "Please enter a valid e-mail address<br/>";
  9. }

Please help me on this......
Last edited by peter_budo; Dec 18th, 2008 at 3:24 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 94
Reputation: sikka_varun is an unknown quantity at this point 
Solved Threads: 11
sikka_varun's Avatar
sikka_varun sikka_varun is offline Offline
Junior Poster in Training

Re: Error in php form validation

 
0
  #5
Dec 18th, 2008
Hi..

Always be very careful when you use so many conditions in if..

Count the no of opening brackets and closing brackets.. opening brackets are greater then closing brackets in if conditions...

Thats why getting { as unexpected error..
Here is the correct code..

  1. <?php
  2. // validate e-mail
  3. if (
  4. (strpos($email, "@") === FALSE) ||
  5. (strpos($email, ".") === FALSE) ||
  6. (strpos($email, " ") != FALSE) || // DON'T KNOW WHY USING "!=" SIGN
  7. (strpos($email, "@") === FALSE) || // DON'T KNOW WHY USING 3 "=" SIGN
  8. (strpos($email, "@") > strrpos($email, "."))
  9. )
  10. {
  11. $error = "Please enter a valid e-mail address<br/>";
  12. }
  13. ?>
VâRûN
---Happy to Help---
sikka_varun@yahoo.com
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 7
Reputation: rajesh.bl is an unknown quantity at this point 
Solved Threads: 0
rajesh.bl rajesh.bl is offline Offline
Newbie Poster

Re: Error in php form validation

 
0
  #6
Dec 18th, 2008
Hi

Thank you very much for ur support.
Everything is fine the code is working but the errors are not able to display for a particular field.

I want to validate every field and if it is not valid i need to display the error message for that particular filed this was not happening.

Kindly request to guide me with code to display error..

Waiting for the reply.....
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 75
Reputation: mustafaneguib is an unknown quantity at this point 
Solved Threads: 3
mustafaneguib mustafaneguib is offline Offline
Junior Poster in Training

Re: Error in php form validation

 
0
  #7
Dec 18th, 2008
heres the programming part of the code. i have fixed it for u and have also commented in areas where i thought that ought to be explained.

however, i haven't checked if the code is working on the system, but i have done what i could do.

  1. <?php include("menu.inc");
  2.  
  3. include("tiki-setup.php");
  4.  
  5. ?>
  6. <div id="centerContent">
  7.  
  8. <p class="first-letter">Please fill the following form in for any enquiries that you may have:</p>
  9. <p>
  10.  
  11. <?php
  12.  
  13. // if the form has been posted, analyse it:
  14.  
  15. if ($_POST) {
  16.  
  17. foreach ($_POST as $field => $value) {
  18.  
  19. $value = trim($value);
  20. }
  21.  
  22.  
  23.  
  24. // creating variables
  25.  
  26. //if the user has just entered space in the field and has not enetered any text then trim will remove that space. basically trim removes leading spaces.
  27.  
  28. $inquiry=trim($_POST['inquiry']);
  29.  
  30. $title=trim($_POST['title']);
  31.  
  32. $first_name=trim($_POST['first_name']);
  33.  
  34. $last_name=trim($_POST['last_name']);
  35.  
  36. $email=trim($_POST['email']);
  37.  
  38. $phone=trim($_POST['phone']);
  39.  
  40. $message=trim($_POST['message']);
  41.  
  42. $reply=trim($_POST['reply']);
  43.  
  44. $contact=trim($_POST['contact']);
  45.  
  46.  
  47.  
  48. // create empty error variable
  49.  
  50. // $error = " "; //write space for empty values.... but in php you do not have to declare a variable.
  51.  
  52.  
  53.  
  54. // check for data in required fields
  55.  
  56.  
  57. //i am not exactly sure if you can use FALSE in the conditions because i use the NOT operator (!) to check if any field is invalid. though i have left that unchanged. i have fixed the brakets problem as well.
  58.  
  59. //i am using empty() function to check if the variables are empty or not. the way you were checking might not have worked for empty values, because usually we write space for empty values but you used something else. so i thing that it was here why your individual fields were not being checked properly.
  60.  
  61. if (empty($inquiry ) || empty($title ) || empty($first_name ) || empty($last_name) || empty($email) || empty($phone ) || empty($message) || empty($reply ) || empty($contact ))
  62. {
  63.  
  64. $error = "Please fill in all the required fields!";
  65.  
  66. } //if any field is empty, that is nothing has been typed in the field then display an error.
  67.  
  68.  
  69.  
  70. // validate first_name
  71.  
  72. if (ctype_alpha($first_name) == FALSE))
  73. {
  74.  
  75. $error = "Please enter a valid name (Alphabets only)<br/>";
  76.  
  77. }
  78.  
  79.  
  80.  
  81. // validate surname
  82.  
  83. if (ctype_alpha($last_name) == FALSE)
  84. {
  85.  
  86. $error = "Please enter a valid last name (Alphabets only)<br/>";
  87.  
  88. }
  89.  
  90.  
  91.  
  92. // validate e-mail
  93.  
  94. if (
  95.  
  96. (strpos($email, "@") == FALSE) ||
  97.  
  98. (strpos($email, ".") == FALSE) ||
  99.  
  100. (strpos($email, " ") != FALSE) || // != means not equals to.
  101.  
  102. (strpos($email, "@") == FALSE) || // you will use == only. using === is syntax error as far as i know.
  103.  
  104. (strpos($email, "@") > strrpos($email, ".")))
  105.  
  106. {
  107.  
  108. $error = "Please enter a valid e-mail address<br/>";
  109.  
  110. }
  111.  
  112.  
  113.  
  114. // validate phone
  115.  
  116. if (is_numeric($phone) == FALSE)
  117. {
  118.  
  119. $error = "Please enter a valid contact number (must contain numbers only)<br/>";
  120.  
  121. }
  122.  
  123.  
  124.  
  125. //If everything is OK then Confirmation of each field's data as filled by the user
  126.  
  127. //do not use else for the moment. you have not included an if statement to go with the else statement. right now the following lines will execute when all your checks have been made.
  128. // else {
  129.  
  130. echo "<p><b>Nature of Inquiry:</b> $inquiry<br/>";
  131.  
  132. echo "<p><b>Title:</b> $title<br/>";
  133.  
  134. echo "<p><b>First Name:</b> $first_name<br/>";
  135.  
  136. echo "<p><b>Last Name:</b> $last_name<br/>";
  137.  
  138. echo "<p><b>E-mail:</b> $email<br/>";
  139.  
  140. echo "<p><b>Contact No.:</b> $phone<br/>";
  141.  
  142. echo "<p><b>Message:</b> $message<br/>";
  143.  
  144. echo "<p><b>Reply:</b> $reply<br/>";
  145.  
  146. echo "<p><b>Contact Method:</b> $contact<br/>";
  147.  
  148.  
  149.  
  150. echo "Thanks for contacting us. We will get back to you shortly!";
  151.  
  152. //}
  153.  
  154. }
  155.  
  156. $smarty->assign('mid', 'tiki-validate.tpl');
  157.  
  158. $smarty->display("tiki.tpl");
  159.  
  160. ?>
  161.  
  162. </p>
Last edited by mustafaneguib; Dec 18th, 2008 at 8:17 pm.
If one has not had the chance to do assembly prgramming then he has not had the chance to do programming at all.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,405
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 225
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Error in php form validation

 
0
  #8
Dec 18th, 2008
=== is not a syntax error. It checks to make sure they are the same value and the same type.
  1. $number = 1;
  2. $string = "1";
  3. if($number === $string) {
  4. echo "They match!"; // This won't happen
  5. } else {
  6. echo "They don't match!"; // This will happen
  7. }
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 7
Reputation: rajesh.bl is an unknown quantity at this point 
Solved Threads: 0
rajesh.bl rajesh.bl is offline Offline
Newbie Poster

Re: Error in php form validation

 
0
  #9
Dec 19th, 2008
Hi,

The code for form validation is working and i need to save the data into database once the data is validated.

can anyone please help me with the code to add in form validation code..

Advance thanx....
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 94
Reputation: sikka_varun is an unknown quantity at this point 
Solved Threads: 11
sikka_varun's Avatar
sikka_varun sikka_varun is offline Offline
Junior Poster in Training

Re: Error in php form validation

 
0
  #10
Dec 20th, 2008
Hi..
Here's the code to add to the database

  1. <?php
  2. // if the form has been posted, analyse it:
  3. if ($_POST) {
  4. foreach ($_POST as $field => $value) {
  5. $value = trim($value);
  6. }
  7.  
  8. // creating variables
  9. $name=$_POST['name'];
  10. $offadd=$_POST['offadd'];
  11. $boardtelno=$_POST['boardtelno'];
  12. $admmail=$_POST['admmail'];
  13. $contacts1=$_POST['contacts1'];
  14. $contacts2=$_POST['contacts2'];
  15. $billingadd=$_POST['billingadd'];
  16.  
  17. // create empty error variable
  18. $error = "";
  19.  
  20. // check for data in required fields
  21. if (($name == "") || ($offadd == "") || ($boardtelno == "") || ($admmail == "") || ($contacts1 == "") || ($contacts2 == "") || ($billingadd == "")) {
  22. //echo $error = "Please fill in all the required fields!";//
  23. $smarty->assign('msg', tra("Please fill in all the required fields!"));
  24. $smarty->display("error.tpl");
  25. die;
  26. display("form");
  27. }
  28.  
  29. else
  30. if ((ctype_alpha($name) == FALSE))
  31. echo $error = "Please enter a valid name (Alphabets only)<br/>";
  32. if ((ctype_alpha($offadd) == FALSE)) {
  33. echo $error = "Please enter a valid off address (Alphabets only)<br/>";
  34. }
  35. if (is_numeric($boardtelno) == FALSE ) {
  36. echo $error = "Please enter a valid boardtelno (must contain numbers only)<br/>";
  37. }
  38. if (
  39. (strpos($admmail, "@") === FALSE) ||
  40. (strpos($admmail, ".") === FALSE) ||
  41. (strpos($admmail, " ") != FALSE) || // DON'T KNOW WHY USING "!=" SIGN
  42. (strpos($admmail, "@") === FALSE) || // DON'T KNOW WHY USING 3 "=" SIGN
  43. (strpos($admmail, "@") > strrpos($admmail, "."))
  44. )
  45. {
  46. echo $error = "Please enter a valid e-mail address<br/>";
  47. }
  48. if (is_numeric($contacts1) == FALSE ) {
  49. echo $error = "Please enter a valid contacts1 number (must contain numbers only)<br/>";
  50.  
  51. }
  52. if (is_numeric($contacts2) == FALSE ) {
  53. echo $error = "Please enter a valid contacts2 number (must contain numbers only)<br/>";
  54.  
  55. }
  56. if ((ctype_alpha($billingadd) == FALSE)) {
  57. echo $error = "Please enter a valid billing address (Alphabets only)<br/>";
  58.  
  59. }
  60.  
  61. /////////////////////////////
  62. //Adding the data to database
  63. /////////////////////////////
  64.  
  65. //Enter your mysql server details
  66. $hostname="localhost";
  67. $username="root";
  68. $password="";
  69. $database_name="forms";
  70.  
  71. //Step1 : Connect to the database
  72. mysql_connect($hostname,$username,$password) or die("Error in connection".mysql_error());
  73. //Step2: Select the database
  74. mysql_select_db($database_name) or die("Error in database selection".mysql_error());
  75.  
  76. //Step3: Write the sql query to insert into the database
  77. $sql = "insert into form_table(name,offadd,boardtelno,admmail,contacts1,contacts2,billingadd)
  78. values('$name','$offadd','$boardtelno','$admmail','$contacts1','$contacts2','$billingadd')";
  79.  
  80. //Step4: Finally execute this query
  81. mysql_query($sql);
  82.  
  83. echo "Successfully written the following data to database<br />";
  84. echo "<p><b>Name:</b> $name<br/>";
  85. echo "<p><b>Offadd:</b> $offadd<br/>";
  86. echo "<p><b>boardtelno:</b> $boardtelno<br/>";
  87. echo "<p><b>admmail:</b> $admmail<br/>";
  88. echo "<p><b>contacts1:</b> $contacts1<br/>";
  89. echo "<p><b>Contacts2.:</b> $contacts2<br/>";
  90. echo "<p><b>Billingadd:</b> $billingadd<br/>";
  91. display("form");
  92. }
  93. ?>
VâRûN
---Happy to Help---
sikka_varun@yahoo.com
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC