Jump to content

What would be the best way of checking user input on a multipage registration?


Namtip

Recommended Posts

Set up:

    * XAMPP 1.7.3

    * Apache 2.2.14 (IPv6 enabled) + OpenSSL 0.9.8l

    * MySQL 5.1.41 + PBXT engine

    * PHP 5.3.1

    * phpMyAdmin 3.2.4

    * Perl 5.10.1

    * FileZilla FTP Server 0.9.33

    * Mercury Mail Transport System 4.72

 

I'm trying to set up a multipage registration script. It's tuff! I've set up some basic scripts to distribute variables into the correct tables from previous forms using a session. But I want the script to check the input from form one is valid before it moves on to form 2.

 

Here are my scripts:

form 1:

<html>
<head>
  <title>Register</title>
  <style type="text/css">
   td { vertical-align: top; }
  </style>
</head>
<body>
  <form action="form2.php" method="post">
   <table>
    <tr>
     <td><label for="name">Username:</label></td>
     <td><input type="text" name="name" id="name" size="20"
       maxlength="20" value=""/></td>
    </tr><tr>
     <td><label for="password">Password:</label></td>
     <td><input type="password" name="password" id="password" size="20"
       maxlength="20" value=""/></td>
    </tr><tr>	   
     <td><label for="first_name">First name:</label></td>
     <td><input type="text" name="first_name" id="first_name" size="20"
       maxlength="20" value=""/></td>
    </tr><tr>
     <td><label for="last_name">Last name:</label></td>
     <td><input type="text" name="last_name" id="last_name" size="20"
       maxlength="20" value=""/></td>
    </tr><tr>
     <td><label for="email">Email:</label></td>
     <td><input type="text" name="email" id="email" size="20" maxlength="50"
       value=""/></td>
    </tr><tr>
     <td><label for="address">Address:</label></td>
     <td><input type="text" name="address" id="address" size="20" maxlength="20"
       value=""/></td>
    </tr><tr>
     <td><label for="city">City/Town:</label></td>
     <td><input type="text" name="city" id="city" size="20" maxlength="20"
       value=""/></td>
    </tr><tr>
 <td><label for="county">County:</label></td>
     <td><input type="text" name="county" id="county" size="20" maxlength="20"
       value=""/></td>
    </tr><tr>
 <td><label for="post">Postcode:</label></td>
     <td><input type="text" name="post" id="post" size="20" maxlength="20"
       value=""/></td>
    </tr><tr>	
 <td><label for="home">Home Number:</label></td>
     <td><input type="text" name="home" id="home" size="20" maxlength="20"
       value=""/></td>
    </tr><tr>	
 <td><label for="mobile">Mobile:</label></td>
     <td><input type="text" name="mobile" id="mobile" size="20" maxlength="20"
       value=""/></td>
    </tr><tr>	
     <td> </td>
     <td><input type="submit" name="submit" value="Sumbit"/></td>
    </tr>
   </table>
  </form>
</body>
</html>

 

Form 2:

<?php

//let's start the session
session_start();

//now, let's register our session variables
session_register('name');
session_register('password');
session_register('first_name');
session_register('last_name');
session_register('email');
session_register('address');
session_register('city');
session_register('county');
session_register('post');
session_register('home');
session_register('mobile');

//finally, let's store our posted values in the session variables
$_SESSION['name'] = $_POST['name'];
$_SESSION['password'] = $_POST['password'];
$_SESSION['first_name'] = $_POST['first_name'];
$_SESSION['last_name'] = $_POST['last_name'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['address'] = $_POST['address'];
$_SESSION['city'] = $_POST['city'];
$_SESSION['county'] = $_POST['county'];
$_SESSION['post'] = $_POST['post'];
$_SESSION['home'] = $_POST['home'];
$_SESSION['mobile'] = $_POST['mobile'];

?>

<html>
<head>
  <title>Register</title>
  <style type="text/css">
   td { vertical-align: top; }
  </style>
</head>
<body>
  <form action="form3.php" method="post">
   <table>
    <tr>
     <td><label for="bio">Biography:</label></td>
     <td><input type="text" name="bio" id="bio" size="400"
       maxlength="500" value=""/></td>
    </tr><tr>	   
     <td> </td>
     <td><input type="submit" name="submit" value="Sumbit"/></td>
    </tr>
   </table>
  </form>
</body>
</html>

 

I've also got form3.php and process_forms.php(that's where I mysql_real_escape_string and input the data) but that's probably not relevant. How would I get this to work? Are there any sites I should look at that you'd recommend? Any help appreciated.

Link to comment
Share on other sites

If I am right you actually want to validate the user input at each step before allowing the user to move onto next..

 

-- Probably this is what you said ---  :-\

 

This is actually very simple all you need to do is to put some validation functions in the start and if the output comes true then move onto next..

 

A basic Example..

 

Form 1: Exactly what you made except few changes...

 

 <html>
<head>
  <title>Register</title>
  <style type="text/css">
   td { vertical-align: top; }
  </style>
</head>
<body>
  <form action="form2.php" method="post">
   <table>
    <tr>
     <td><label for="name">Username:</label></td>
     <td><input type="text" name="name" id="name" size="20"
       maxlength="20" value="if(isset($_SESSION['name'])) {echo $_SESSION['name']; }else{ } "/></td>
    </tr>

      </table>
  </form>
</body>
</html>

 

Did you notice the change. This will keep the user input intact even if we return after an error occurs. Implement it to all fields. Plz dont do that with password field..

 

 

NOw validation..

 

Form 2:

 

<?php

//let's start the session
session_start();

//now, let's register our session variables
session_register('name');
session_register('password');
session_register('first_name');
session_register('last_name');
session_register('email');
session_register('address');
session_register('city');
session_register('county');
session_register('post');
session_register('home');
session_register('mobile');

//finally, let's store our posted values in the session variables
$_SESSION['name'] = $_POST['name'];
$_SESSION['password'] = $_POST['password'];
$_SESSION['first_name'] = $_POST['first_name'];
$_SESSION['last_name'] = $_POST['last_name'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['address'] = $_POST['address'];
$_SESSION['city'] = $_POST['city'];
$_SESSION['county'] = $_POST['county'];
$_SESSION['post'] = $_POST['post'];
$_SESSION['home'] = $_POST['home'];
$_SESSION['mobile'] = $_POST['mobile'];

if(  preg_match('/[a-zA-Z]+/',$_SESSION['name']) ) {

} else{

header("Location: form1.php");
exit;

}









?>

<html>
<head>
  <title>Register</title>
  <style type="text/css">
   td { vertical-align: top; }
  </style>
</head>
<body>
  <form action="form3.php" method="post">
   <table>
    <tr>
     <td><label for="bio">Biography:</label></td>
     <td><input type="text" name="bio" id="bio" size="400"
       maxlength="500" value=""/></td>
    </tr><tr>



   
     <td> </td>
     <td><input type="submit" name="submit" value="Sumbit"/></td>
    </tr>
   </table>
  </form>
</body>
</html>

 

You need to place similar checks on all field. This way user input is validated before user is shown second form.

Link to comment
Share on other sites

Thanks guys!

JCBones, I checked out ajax with this awesome tutorial:http://www.9lessons.info/2008/12/twitter-used-jquery-plug-in.html which I got working, but I need to get better at php before I start to use that stuff.

 

For anyone that is interested in this post I recommend the following sites

http://www.webcheatsheet.com/php/regular_expressions.php#replace

http://regexlib.com/

 

and the parse error: unmatched parentheses means you have one more ")" in your regular expressions than you have "(".

the parse error: preg_match() [function.preg-match]: No ending delimiter '^' means you need to end the regular expression with the same character that you started it with.

 

but I'm having problems with my scripts not doing what I intended them to do.

form1.php

<html>
<head>
  <title>Register</title>
  <style type="text/css">
   td { vertical-align: top; }
  </style>
</head>
<body>
  <form action="form2.php" method="post">
   <table>
    <tr>
     <td><label for="name">Username:</label></td>
     <td><input type="text" name="name" id="name" size="20"
       maxlength="20" value="<?php if(isset($_SESSION['name'])) {echo $_SESSION['name']; }else{ } ?>"/>
   <?php  if(isset($_POST[$errname])) echo $errname; ?></td>
</tr><tr>
     <td><label for="password">Password:</label></td>
     <td><input type="password" name="password" id="password" size="20"
       maxlength="20" value=""/>
   </td>
   
    </tr><tr>	   
     <td><label for="first_name">First name:</label></td>
     <td><input type="text" name="first_name" id="first_name" size="20"
       maxlength="20" value="<?php if(isset($_SESSION['first_name'])) {echo $_SESSION['first_name']; }else{ } ?>"/>
   <?php  if(isset($_POST[$errfirst])) echo $errfirst; ?></td>
    </tr><tr>
     <td><label for="last_name">Last name:</label></td>
     <td><input type="text" name="last_name" id="last_name" size="20"
       maxlength="20" value="<?php if(isset($_SESSION['last_name'])) {echo $_SESSION['last_name']; }else{ } ?>"/>
   <?php  if(isset($_POST[$errlast])) echo $errlast; ?>
   </td>
    </tr><tr>
     <td><label for="email">Email:</label></td>
     <td><input type="text" name="email" id="email" size="20" maxlength="50"
       value="<?php if(isset($_SESSION['email'])) {echo $_SESSION['email']; }else{ } ?>"/>
   <?php  if(isset($_POST[$erremail])) echo $erremail; ?></td>
    </tr><tr>
     <td><label for="address">Address:</label></td>
     <td><input type="text" name="address" id="address" size="20" maxlength="20"
       value="<?php if(isset($_SESSION['address'])) {echo $_SESSION['address']; }else{ } ?>"/>
   <?php  if(isset($_POST[$erraddress])) echo $erraddress; ?></td>
    </tr><tr>
     <td><label for="city">City/Town:</label></td>
     <td><input type="text" name="city" id="city" size="20" maxlength="20"
       value="<?php if(isset($_SESSION['city'])) {echo $_SESSION['city']; }else{ } ?>"/>
   <?php  if(isset($_POST[$errcity])) echo $errcity; ?></td>
    </tr><tr>
 <td><label for="county">County:</label></td>
     <td><input type="text" name="county" id="county" size="20" maxlength="20"
       value="<?php if(isset($_SESSION['county'])) {echo $_SESSION['county']; }else{ } ?>"/>
   <?php  if(isset($_POST[$errcounty])) echo $errcounty; ?></td>
    </tr><tr>
 <td><label for="post">Postcode:</label></td>
     <td><input type="text" name="post" id="post" size="20" maxlength="20"
       value="<?php if(isset($_SESSION['post'])) {echo $_SESSION['post']; }else{ } ?>"/>
   <?php  if(isset($_POST[$errpost])) echo $errpost; ?></td>
    </tr><tr>	
 <td><label for="home">Home Number:</label></td>
     <td><input type="text" name="home" id="home" size="20" maxlength="20"
       value="<?php if(isset($_SESSION['home'])) {echo $_SESSION['home']; }else{ } ?>"/>
   <?php  if(isset($_POST[$errhome])) echo $errhome; ?></td>
    </tr><tr>	
 <td><label for="mobile">Mobile:</label></td>
     <td><input type="text" name="mobile" id="mobile" size="20" maxlength="20"
       value="<?php if(isset($_SESSION['mobile'])) {echo $_SESSION['mobile']; }else{ } ?>"/>
   <?php  if(isset($_POST[$errmobile])) echo $errmobile; ?></td>
    </tr><tr>	
     <td> </td>
     <td><input type="submit" name="submit" value="Sumbit"/></td>
    </tr>
   </table>
  </form>
</body>
</html>

form2.php

<?php

//let's start the session
session_start();

//finally, let's store our posted values in the session variables
$_SESSION['name'] = $_POST['name'];
$_SESSION['password'] = $_POST['password'];
$_SESSION['first_name'] = $_POST['first_name'];
$_SESSION['last_name'] = $_POST['last_name'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['address'] = $_POST['address'];
$_SESSION['city'] = $_POST['city'];
$_SESSION['county'] = $_POST['county'];
$_SESSION['post'] = $_POST['post'];
$_SESSION['home'] = $_POST['home'];
$_SESSION['mobile'] = $_POST['mobile'];

$errname     = "";
$errfirst  = "";
$errlast  = "";
$erremail    = "";
$erraddress = "";
$errcity    = "";
$errcounty      = "";
$errpost     = "";
$errhome     = "";
$errmob     = "";
  
if(  preg_match('/^[A-Z][a-zA-Z -]{3,30}+$/',$_SESSION['name']) ) {
$errname = '<p class="errText">Name must be from letters, dashes, spaces and must not start with dash</p>';
}
if(  preg_match('/^[A-Z][a-zA-Z -]{3,30}+$/',$_SESSION['first_name']) ) {
$errfirst = '<p class="errText">Name must be from letters, dashes, spaces and must not start with dash</p>';
}
if(  preg_match('/^[A-Z][a-zA-Z -]{3,30}+$/',$_SESSION['last_name']) ) {
$errlast = '<p class="errText">Name must be from letters, dashes, spaces and must not start with dash</p>';
}
if(  preg_match('/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/',$_SESSION['email']) ) {
$erremail    = '<p class="errText">This is not a valid email address.';
}
if(  preg_match('/^[a-zA-Z0-9 _.,:\"\']+$/',$_SESSION['address']) ) {
$erraddress = '<p class="errText">Address must be only letters, numbers or one of the following ". , : /"</p>';
}
if(  preg_match('/[a-zA-Z]+/',$_SESSION['city']) ) {
$errcity    = '<p class="errText">Your city must contain a letter.';
}
if(  preg_match('/[a-zA-Z]+/',$_SESSION['county']) ) {
$errcounty      = '<p class="errText">Your county must contain a letter.';
}
if(  preg_match('/(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[iJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKSTUW])|([A-Z-[QVX]][A-Z-[iJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})/',$_SESSION['post']) ) {
$errpost     = '<p class="errText">This is not a valid UK Postcode.';
}
if(  preg_match('/s*\(?0\d{4}\)?(\s*|-)\d{3}(\s*|-)(\d{3}\s*)|(\s*\(?0\d{3}\)?(\s*|-)\d{3}(\s*|-)\d{4}\s*)|(\s*)(7|(\d{7}|\d{3}(\-|\s{1})\d{4})\s*/',$_SESSION['home']) ) {
$errhome     = '<p class="errText">This is not a valid UK local phone number.';
}
if(  preg_match('/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/',$_SESSION['mobile']) ) {
$errmob     = '<p class="errText">This is not a valid UK mobile phone number.';
}


else{

$errname     = ($_POST[$errname]);
$errfirst  = ($_POST[$errfirst]);
$errlast  = ($_POST[$errlast]);
$erremail    = ($_POST[$erremail]);
$erraddress = ($_POST[$erraddress]);
$errcity    = ($_POST[$errcity]);
$errcounty      = ($_POST[$errcounty]);
$errpost     = ($_POST[$errpost]);
$errhome     = ($_POST[$errhome]);
$errmob     = ($_POST[$errmob]);
header("Location: form1.php");
exit;

}

?>

<html>
<head>
  <title>Register</title>
  <style type="text/css">
   td { vertical-align: top; }
  </style>
</head>
<body>
  <form action="form3.php" method="post">
   <table>
    <tr>
     <td><label for="bio">Biography:</label></td>
     <td><input type="text" name="bio" id="bio" size="400"
       maxlength="500" value=""/></td>
    </tr><tr>	   
     <td> </td>
     <td><input type="submit" name="submit" value="Sumbit"/></td>
    </tr>
   </table>
  </form>
</body>
</html>

 

I want the error messages to come up in form 1 with the session information from form 2.

*edit*

but it just sends me back to form one with no error messages or session info in the forms.

Link to comment
Share on other sites

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.