Jump to content

Session variables do not get returned into previous page's form.


Namtip

Recommended Posts

This is a multi page registration form. I want to check the user input from form1.php with preg_match in form2.php then return the users details back into form1 with error messages(e.g $errname) so the user doesn't need to re enter the information.

 

Neither the error messages or the session variables are shown in form1.php's form. I've echoed out the session variables found that the value makes it to form 2. But the variables are empty when it goes back to form 1.

 

form1.php

<html>
<head>
  <title>Register</title>
  <style type="text/css">
   td { vertical-align: top; }
    .errText {
    	font-family: Arial;
    	font-size: 10px;
    	color: #CC0000;
    	text-decoration: none;
    	font-weight: normal;
    }
  </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 echo $_SESSION['name'];?>"/>
   <?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 echo $_SESSION['first_name'];?>"/>
   <?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 echo $_SESSION['last_name'];?>"/>
   <?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 echo $_SESSION['email'];?>"/>
   <?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 echo $_SESSION['address'];?>"/>
   <?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 echo $_SESSION['city'];?>"/>
   <?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 echo $_SESSION['county'];?>"/>
   <?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 echo $_SESSION['post'];?>"/>
   <?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 echo $_SESSION['home'];?>"/>
   <?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 echo $_SESSION['mobile'];?>"/>
   <?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;

}

?>

Any help appreciated. I think I should buy a professional book, instead of my beginner one. :shy:

 

Link to comment
Share on other sites

Ah! good spot! I could kiss you! That's go the sessions to pass through to form1 but my error messages still aren't coming up :(. Any clues?

 

Well, I don't know all of your code, but form2 is not making any sense.  I haven't tested, but this is more like what it should be I think:

 

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

//let's store our posted values in the session variables
$_SESSION['values'] = $_POST;

//let's check for errors
$error = array();

if(  preg_match('/^[A-Z][a-zA-Z -]{3,30}+$/',$_POST['name']) ) {
$error['name'] = '<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}+$/',$_POST['first_name']) ) {
$error['first'] = '<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}+$/',$_POST['last_name']) ) {
$error['last'] = '<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}$/',$_POST['email']) ) {
$error['email']    = '<p class="errText">This is not a valid email address.';
}
if(  preg_match('/^[a-zA-Z0-9 _.,:\"\']+$/',$_POST['address']) ) {
$error['address'] = '<p class="errText">Address must be only letters, numbers or one of the following ". , : /"</p>';
}
if(  preg_match('/[a-zA-Z]+/',$_POST['city']) ) {
$error['city']    = '<p class="errText">Your city must contain a letter.';
}
if(  preg_match('/[a-zA-Z]+/',$_POST['county']) ) {
$error['county']      = '<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})/',$_POST['post']) ) {
$error['post']     = '<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*/',$_POST['home']) ) {
$error['home']     = '<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}$/',$_POST['mobile']) ) {
$error['mob']     = '<p class="errText">This is not a valid UK mobile phone number.';
}

//if we have errors let's assign them to the session and redirect back, you could just assign to $_SESSION['error']['name'] etc.. above
if(!empty($error)) {
$_SESSION['error'] = $error;
header("Location: form1.php");
exit;
}

 

So in form1 they would each need to look something like this:

 

<td><label for="name">Username:</label></td>
<td><input type="text" name="name" id="name" size="20" maxlength="20" value="<?php echo isset($_SESSION['values']['name']) ? $_SESSION['values']['name'] : ""; ?>"/>
<?php echo isset($_SESSION['error']['name']) ? $_SESSION['error']['name'] : ""; ?></td>

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.