form validation with select option data + input data

Reply

Join Date: Sep 2009
Posts: 3
Reputation: wandor is an unknown quantity at this point 
Solved Threads: 0
wandor wandor is offline Offline
Newbie Poster

form validation with select option data + input data

 
0
  #1
Sep 6th, 2009
When I fill my Contact Form

1) select option TITLE (MR. MRS. MISS)
2)First Name
3)Last Name
4) E-mail

I make validation to all data, The question is when I select title e.g. MR and then fill first name, last name AND e-mail IS NOT right (info2company.com) validation said *Email invalid, then I write my e-mail right (info@company.com) and click SEND button then validation said * title not entered.

I wanna hold my option data please help, I fight 3 days now...

here is my code:

register.php

  1. <?php
  2. /**
  3.  * Register.php
  4.  * Last Updated: 5-9-2009
  5.  */
  6. include("include/session.php");
  7. ?>
  8.  
  9. <html>
  10. <title>Registration Page</title>
  11. <body>
  12.  
  13. <?php
  14.  
  15. if(isset($_SESSION['regsuccess'])){
  16. /* Registration was successful */
  17. if($_SESSION['regsuccess']){
  18. echo "<h1>Registered!</h1>";
  19. echo "<p>Thank you <b>".$_SESSION['reguname']."</b>, "
  20. ."go back to <a href=\"register.php\">register page</a>.</p>";
  21. }
  22. /* Registration failed */
  23. else{
  24. echo "<h1>Registration Failed</h1>";
  25. echo "<p>We're sorry, but an error has occurred
  26. ."<br>Please try again at a later time.</p>";
  27. }
  28. unset($_SESSION['regsuccess']);
  29. unset($_SESSION['reguname']);
  30. }
  31.  
  32. else{
  33. ?>
  34.  
  35. <h1>Contact Form</h1>
  36. <?php
  37. if($form->num_errors > 0){
  38. echo "<td><font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font></td>";
  39.  
  40. }
  41. ?>
  42. <form action="process.php" method="POST">
  43. <table align="left" border="1" cellspacing="0" cellpadding="3">
  44.  
  45. <tr>
  46. <td>Title:</td>
  47.  
  48. <td>
  49. <select name="title" value="<?php echo $form->value("title"); ?>">
  50. <option value="">Select One</option>
  51. <option value="Mr">Mr.</option>
  52. <option value="Mrs">Mrs.</option>
  53. <option value="Ms">Ms.</option>
  54. </select>
  55. </td>
  56. <td><?php echo $form->error("title"); ?></td>
  57. </tr>
  58.  
  59. <tr>
  60. <td>First Name:</td>
  61. <td><input type="text" name="firstname" maxlength="30" value="<?php echo $form->value("firstname"); ?>"></td>
  62. <td><?php echo $form->error("firstname"); ?></td>
  63. </tr>
  64.  
  65. <tr>
  66. <td>Last Name:</td>
  67. <td><input type="text" name="lastname" maxlength="30" value="<?php echo $form->value("lastname"); ?>"></td>
  68. <td><?php echo $form->error("lastname"); ?></td>
  69. </tr>
  70.  
  71.  
  72.  
  73. <tr>
  74. <td>Email:</td>
  75. <td><input type="text" name="email" maxlength="50" value="<?php echo $form->value("email"); ?>"></td>
  76. <td><?php echo $form->error("email"); ?></td>
  77. </tr>
  78.  
  79. <tr>
  80. <td colspan="2" align="right">
  81. <input type="hidden" name="subjoin" value="1">
  82. <input type="submit" value="Send!"></td>
  83. </tr>
  84.  
  85. <tr>
  86. <td colspan="2" align="left"><a href="main.php">Back to Main</a></td>
  87. </tr>
  88.  
  89. </table>
  90. </form>
  91.  
  92. <?php
  93. }
  94. ?>
  95.  
  96. </body>
  97. </html>


process.php

  1. <?php
  2. /**
  3.  * Process.php
  4.  * Last Updated: 5-9-2009
  5.  */
  6. include("include/session.php");
  7.  
  8. class Process
  9. {
  10. /* Class constructor */
  11. function Process(){
  12. global $session;
  13.  
  14. /* User submitted registration form */
  15. if(isset($_POST['subjoin'])){
  16. $this->procRegister();
  17. }
  18.  
  19. else{
  20. header("Location: register1.php");
  21. }
  22. }
  23.  
  24. function procRegister(){
  25. global $session, $form;
  26. /* Convert username to all lowercase (by option) */
  27. if(ALL_LOWERCASE){
  28. $_POST['title'] = strtolower($_POST['title']);
  29. }
  30. /* Registration attempt */
  31. $retval = $session->register($_POST['title'],$_POST['firstname'],$_POST['lastname'],$_POST['email']);
  32.  
  33. /* Registration Successful */
  34. if($retval == 0){
  35. $_SESSION['reguname'] = $_POST['title'];
  36. $_SESSION['regsuccess'] = true;
  37. header("Location: ".$session->referrer);
  38. }
  39. /* Error found with form */
  40. else if($retval == 1){
  41. $_SESSION['value_array'] = $_POST;
  42. $_SESSION['error_array'] = $form->getErrorArray();
  43. header("Location: ".$session->referrer);
  44. }
  45. /* Registration attempt failed */
  46. else if($retval == 2){
  47. $_SESSION['reguname'] = $_POST['title'];
  48. $_SESSION['regsuccess'] = false;
  49. header("Location: ".$session->referrer);
  50. }
  51. }
  52. };
  53.  
  54. /* Initialize process */
  55. $process = new Process;
  56.  
  57. ?>


include/session.php


  1. <?php
  2. /**
  3.  * Session.php
  4.  * Last Updated: 5-9-2009
  5.  */
  6. include("database.php");
  7. include("form.php");
  8.  
  9. class Session
  10. {
  11. var $title;
  12. var $time;
  13. var $url;
  14. var $referrer;
  15.  
  16.  
  17. /* Class constructor */
  18. function Session(){
  19. $this->time = time();
  20. $this->startSession();
  21. }
  22.  
  23. function startSession(){
  24. global $database; //The database connection
  25. session_start(); //Tell PHP to start the session
  26.  
  27. /* Set referrer page */
  28. if(isset($_SESSION['url'])){
  29. $this->referrer = $_SESSION['url'];
  30. }else{
  31. $this->referrer = "/";
  32. }
  33. /* Set current url */
  34. $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
  35. }
  36.  
  37. function register($subtitle,$subfirstname,$sublastname,$subemail){
  38. global $database, $form, $mailer; //The database, form and mailer object
  39.  
  40. /* title error checking */
  41. $field = "title"; //Use field name for title
  42. if(!$subtitle || strlen($subtitle = trim($subtitle)) == 0){
  43. $form->setError($field, "* title not entered");
  44. }
  45. else{
  46. /* Spruce up title, check length */
  47. $subtitle = stripslashes($subtitle);
  48.  
  49. }
  50.  
  51. /* First Name error checking */
  52. $field = "firstname"; //Use field name for firstname
  53. if(!$subfirstname || strlen($subfirstname = trim($subfirstname)) == 0){
  54. $form->setError($field, "* firstname not entered");
  55. }
  56. else{
  57. /* Spruce up firstname, check length */
  58. $subfirstname = stripslashes($subfirstname);
  59. if(strlen($subfirstname) < 5){
  60. $form->setError($field, "* firstname below 5 characters");
  61. }
  62. else if(strlen($subfirstname) > 30){
  63. $form->setError($field, "* firstname above 30 characters");
  64. }
  65. /* Check if firstname is not alphanumeric */
  66. else if(!eregi("^([0-9a-z])+$", $subfirstname)){
  67. $form->setError($field, "* firstname not alphanumeric");
  68. }
  69. }
  70. /* Last Name error checking */
  71. $field = "lastname"; //Use field name for lastname
  72. if(!$sublastname || strlen($sublastname = trim($sublastname)) == 0){
  73. $form->setError($field, "* lastname not entered");
  74. }
  75. else{
  76. /* Spruce up lastname, check length */
  77. $sublastname = stripslashes($sublastname);
  78. if(strlen($sublastname) < 5){
  79. $form->setError($field, "* lastname below 5 characters");
  80. }
  81. else if(strlen($sublastname) > 30){
  82. $form->setError($field, "* lastname above 30 characters");
  83. }
  84. /* Check if lastname is not alphanumeric */
  85. else if(!eregi("^([0-9a-z])+$", $sublastname)){
  86. $form->setError($field, "* lastname not alphanumeric");
  87. }
  88. }
  89.  
  90. /* Email error checking */
  91. $field = "email"; //Use field name for email
  92. if(!$subemail || strlen($subemail = trim($subemail)) == 0){
  93. $form->setError($field, "* Email not entered");
  94. }
  95. else{
  96. /* Check if valid email address */
  97. $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
  98. ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
  99. ."\.([a-z]{2,}){1}$";
  100. if(!eregi($regex,$subemail)){
  101. $form->setError($field, "* Email invalid");
  102. }
  103. $subemail = stripslashes($subemail);
  104. }
  105.  
  106. /* Errors exist, have title correct them */
  107. if($form->num_errors > 0){
  108. return 1; //Errors with form
  109. }
  110. /* No errors, add the new account to the */
  111. else{
  112. if($database->addNewUser($subtitle,$subfirstname,$sublastname,$subemail)){
  113. if(EMAIL_WELCOME){
  114. $mailer->sendWelcome($subtitle,$subfirstname,$sublastname,$subemail);
  115. }
  116. return 0; //New user added succesfully
  117. }else{
  118. return 2; //Registration attempt failed
  119. }
  120. }
  121. }
  122. };
  123.  
  124. $session = new Session;
  125.  
  126. /* Initialize form object */
  127. $form = new Form;
  128.  
  129. ?>
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,449
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 135
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: form validation with select option data + input data

 
0
  #2
Sep 6th, 2009
Try replacing line 42 of include/session.php with the following
  1. $subtitle = trim($subtitle)
  2. if(strlen($subtitle) == 0){
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 3
Reputation: wandor is an unknown quantity at this point 
Solved Threads: 0
wandor wandor is offline Offline
Newbie Poster

Re: form validation with select option data + input data

 
0
  #3
Sep 6th, 2009
I do that but nothing happen, and I think you forgot
;

after
$subtitle = trim($subtitle)
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 3
Reputation: wandor is an unknown quantity at this point 
Solved Threads: 0
wandor wandor is offline Offline
Newbie Poster

Re: form validation with select option data + input data

 
0
  #4
Sep 6th, 2009
cwarn23, I think you send me to right place in file session.php line 45 this is the else and in file register.php.

put <?php echo $form->value("title"); ?> before<select name".....

and when you select e.g. Mr title you see before <select box Mr value, I think I need something like if option value == my selection e.g. Mr make this value selected.

can somebody HELP
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,449
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 135
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: form validation with select option data + input data

 
0
  #5
Sep 7th, 2009
put <?php echo $form->value("title"); ?> before<select name".....

and when you select e.g. Mr title you see before <select box Mr value, I think I need something like if option value == my selection e.g. Mr make this value selected.
I just checked your code and it appears that there is no method/function called value. So perhaps you still need to programme in the value() method.
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Reply

Message:



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