Ofro04 Posted April 19, 2010 Share Posted April 19, 2010 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 Quote Link to comment Share on other sites More sharing options...
aeroswat Posted April 19, 2010 Share Posted April 19, 2010 I think if you do it all on the same page and just use resulting $_POST variables it may be more reliable. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 19, 2010 Share Posted April 19, 2010 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 19, 2010 Share Posted April 19, 2010 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> Quote Link to comment Share on other sites More sharing options...
Ofro04 Posted April 19, 2010 Author Share Posted April 19, 2010 Thanks for your input! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.