Jump to content

Session Variables


Ofro04

Recommended Posts

I currently have a form which posts to different page. This page processes the data in the post variables and if it detects an error it does a redirect back to the original form. I would like to repopulate the form fields with the initial values that the user had entered. I've resolved to do this using session variables but what I would like to know is if there is a better way to do this or if there is a reason that I shouldn't use session variables. I would like to note that this is being done with in a Joomla! application.

 

Thanks for you help

 

Cheers!

 

Ofro04

Link to comment
Share on other sites

A better process, in my opinion, is to always have form scripts POST to themsleves. If validation fails, it is a trivial matter to redisplay the form with the values the user entered. If validation passes, then include() the page that actually processes the data and displays the confirmation page to the user.

Link to comment
Share on other sites

Here is a very simple example:

 

<?php
  
//Only validate if form was submitted
if(isset($_POST))
{
    //Form was submitted, validate data
    $errors = false;
    $fname = trim($_POST['fname']);
    $lname = trim($_POST['lname']);
    $phone = trim($_POST['phone']);

    //Validate data submitted
    if(empty($lname) || empty($phone))
    {
        $errors = "Last name and phone are required.";
    }

    //Validation passed, include processing page
    if(!$errors)
    {
        //Form 1 validation failed, redisplay the form
        include('processForm.php');
        exit();
    }
}
?>
<html>
<body>
<div style="color:#ff0000;"> <?php echo $errors; ?> </div><br />
Please enter your details.<br />
<form name="test" method="POST">
First Name: <input type="text" name="fname" value="<?php echo $_POST['fname']; ?>" /><br />
<b>Last Name:</b> <input type="text" name="lname" value="<?php echo $_POST['lname']; ?>" /><br />
<b>Phone:</b> <input type="text" name="phone" value="<?php echo $_POST['phone']; ?>" /><br />
<button type="submit">Submit</button>
</form>
* Required fields in bold<br />
</form>
</body>
</html>

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.