form validation with select option data + input data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wandor
Forum Newbie
Posts: 2
Joined: Sat Sep 05, 2009 10:54 am

form validation with select option data + input data

Post by wandor »

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... :banghead:

here is my code:

register.php

Code: Select all

<?php
/**
 * Register.php
 * Last Updated: 5-9-2009
 */
include("include/session.php");
?>
 
<html>
<title>Registration Page</title>
<body>
 
<?php
 
if(isset($_SESSION['regsuccess'])){
   /* Registration was successful */
   if($_SESSION['regsuccess']){
      echo "<h1>Registered!</h1>";
      echo "<p>Thank you <b>".$_SESSION['reguname']."</b>, "
          ."go back to <a href=\"register.php\">register page</a>.</p>";
   }
   /* Registration failed */
   else{
      echo "<h1>Registration Failed</h1>";
      echo "<p>We're sorry, but an error has occurred
          ."<br>Please try again at a later time.</p>";
   }
   unset($_SESSION['regsuccess']);
   unset($_SESSION['reguname']);
}
 
else{
?>
 
<h1>Contact Form</h1>
<?php
if($form->num_errors > 0){
   echo "<td><font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font></td>";
 
}
?>
<form action="process.php" method="POST">
<table align="left" border="1" cellspacing="0" cellpadding="3">
 
<tr>
<td>Title:</td>
 
<td>
<select name="title" value="<?php echo $form->value("title"); ?>">
<option value="">Select One</option>
<option value="Mr">Mr.</option>
<option value="Mrs">Mrs.</option>
<option value="Ms">Ms.</option>
</select>
</td>
<td><?php echo $form->error("title"); ?></td>
</tr>
 
<tr>
<td>First Name:</td>
<td><input type="text" name="firstname" maxlength="30" value="<?php echo $form->value("firstname"); ?>"></td>
<td><?php echo $form->error("firstname"); ?></td>
</tr>
 
<tr>
<td>Last Name:</td>
<td><input type="text" name="lastname" maxlength="30" value="<?php echo $form->value("lastname"); ?>"></td>
<td><?php echo $form->error("lastname"); ?></td>
</tr>
 
 
 
<tr>
<td>Email:</td>
<td><input type="text" name="email" maxlength="50" value="<?php echo $form->value("email"); ?>"></td>
<td><?php echo $form->error("email"); ?></td>
</tr>
 
<tr>
<td colspan="2" align="right">
<input type="hidden" name="subjoin" value="1">
<input type="submit" value="Send!"></td>
</tr>
 
<tr>
<td colspan="2" align="left"><a href="main.php">Back to Main</a></td>
</tr>
 
</table>
</form>
 
<?php
}
?>
 
</body>
</html>

process.php

Code: Select all

<?php
/**
 * Process.php
 * Last Updated: 5-9-2009
 */
include("include/session.php");
 
class Process
{
   /* Class constructor */
   function Process(){
      global $session;
      
      /* User submitted registration form */
      if(isset($_POST['subjoin'])){
         $this->procRegister();
      }
 
       else{
          header("Location: register1.php");
       }
   }
 
   function procRegister(){
      global $session, $form;
      /* Convert username to all lowercase (by option) */
      if(ALL_LOWERCASE){
         $_POST['title'] = strtolower($_POST['title']);
      }
      /* Registration attempt */
      $retval = $session->register($_POST['title'],$_POST['firstname'],$_POST['lastname'],$_POST['email']);
      
      /* Registration Successful */
      if($retval == 0){
         $_SESSION['reguname'] = $_POST['title'];
         $_SESSION['regsuccess'] = true;
         header("Location: ".$session->referrer);
      }
      /* Error found with form */
      else if($retval == 1){
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
         header("Location: ".$session->referrer);
      }
      /* Registration attempt failed */
      else if($retval == 2){
         $_SESSION['reguname'] = $_POST['title'];
         $_SESSION['regsuccess'] = false;
         header("Location: ".$session->referrer);
      }
   }
};
 
/* Initialize process */
$process = new Process;
 
?>
 

include/session.php

Code: Select all

<?php
/**
 * Session.php
 * Last Updated: 5-9-2009
 */
include("database.php");
include("form.php");
 
class Session
{
   var $title;     
   var $time;         
   var $url;          
   var $referrer;     
 
 
   /* Class constructor */
   function Session(){
      $this->time = time();
      $this->startSession();
   }
   
   function startSession(){
      global $database;  //The database connection
      session_start();   //Tell PHP to start the session
 
      /* Set referrer page */
      if(isset($_SESSION['url'])){
         $this->referrer = $_SESSION['url'];
      }else{
         $this->referrer = "/";
      }
      /* Set current url */
      $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
   }
  
   function register($subtitle,$subfirstname,$sublastname,$subemail){
      global $database, $form, $mailer;  //The database, form and mailer object
      
      /* title error checking */
      $field = "title";  //Use field name for title
      if(!$subtitle || strlen($subtitle = trim($subtitle)) == 0){
         $form->setError($field, "* title not entered");
      }
      else{
         /* Spruce up title, check length */
         $subtitle = stripslashes($subtitle);
        
      }
      
/* First Name error checking */
      $field = "firstname";  //Use field name for firstname
      if(!$subfirstname || strlen($subfirstname = trim($subfirstname)) == 0){
         $form->setError($field, "* firstname not entered");
      }
      else{
         /* Spruce up firstname, check length */
         $subfirstname = stripslashes($subfirstname);
         if(strlen($subfirstname) < 5){
            $form->setError($field, "* firstname below 5 characters");
         }
         else if(strlen($subfirstname) > 30){
            $form->setError($field, "* firstname above 30 characters");
         }
         /* Check if firstname is not alphanumeric */
         else if(!eregi("^([0-9a-z])+$", $subfirstname)){
            $form->setError($field, "* firstname not alphanumeric");
         }
      }
/* Last Name error checking */
      $field = "lastname";  //Use field name for lastname
      if(!$sublastname || strlen($sublastname = trim($sublastname)) == 0){
         $form->setError($field, "* lastname not entered");
      }
      else{
         /* Spruce up lastname, check length */
         $sublastname = stripslashes($sublastname);
         if(strlen($sublastname) < 5){
            $form->setError($field, "* lastname below 5 characters");
         }
         else if(strlen($sublastname) > 30){
            $form->setError($field, "* lastname above 30 characters");
         }
         /* Check if lastname is not alphanumeric */
         else if(!eregi("^([0-9a-z])+$", $sublastname)){
            $form->setError($field, "* lastname not alphanumeric");
         }
      }
      
      /* Email error checking */
      $field = "email";  //Use field name for email
      if(!$subemail || strlen($subemail = trim($subemail)) == 0){
         $form->setError($field, "* Email not entered");
      }
      else{
         /* Check if valid email address */
         $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
                 ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
                 ."\.([a-z]{2,}){1}$";
         if(!eregi($regex,$subemail)){
            $form->setError($field, "* Email invalid");
         }
         $subemail = stripslashes($subemail);
      }
 
      /* Errors exist, have title correct them */
      if($form->num_errors > 0){
         return 1;  //Errors with form
      }
      /* No errors, add the new account to the */
      else{
         if($database->addNewUser($subtitle,$subfirstname,$sublastname,$subemail)){
            if(EMAIL_WELCOME){
               $mailer->sendWelcome($subtitle,$subfirstname,$sublastname,$subemail);
            }
            return 0;  //New user added succesfully
         }else{
            return 2;  //Registration attempt failed
         }
      }
   }
};
 
$session = new Session;
 
/* Initialize form object */
$form = new Form;
 
?>
 
wandor
Forum Newbie
Posts: 2
Joined: Sat Sep 05, 2009 10:54 am

Re: form validation with select option data + input data

Post by wandor »

I still looking but nothing, please HELP :banghead:
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: form validation with select option data + input data

Post by Ollie Saunders »

Try writing a method that checks the validity of each field. chkTitle(), chkEmail(), etc. Test them individually and then integrate them into a complete validation process.
Post Reply