Jump to content

form validation with select option data + input data


Recommended Posts

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

 

<?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

 

<?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

 

 

<?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;

?>

 

 

 

I think you are making this too complicated. You could have the form post to the same page, and process on top.

 

if($_POST)
{
     // do validation and output error or thank you message
}

if(!$_POST OR $error)
{
     // show form
    echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
     // form elements go here
   echo '</form>';
}

thank you for post, I make this to practice my knowlege about php clases functions and I think its simple, but my question is <option value validation and hold my select value.

 

I think the problem is here:

 

<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>

 

 

I need something like get my select option value back, can somebody help  :facewall:

You don't set the value of selects that way. the value of a select form comes from the options tags that are its "children" if you want to it have a default option already selected, you have to add the selected attribute to that specific option. In you case you will probably have to have if statements check what the latest value was, and then either make the option selected or not.

mikesta707 thanks for post, did you run my form and undertand my question, if I undertand you mean something like this.

 

<select name="title" value="<?php echo $form->value("title"); ?>">
<option value="">Select One</option>
<option selected = "selected" value="Mr">Mr.</option>
<option value="Mrs">Mrs.</option>
<option value="Ms">Ms.</option>
</select>

 

this is not that I ask (selected = "selected")

 

read my question, play with script and ask me if you don't undertand.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.