jeff5656 Posted May 26, 2010 Share Posted May 26, 2010 If I have a form, and I have aform processing page like process.php and let's say I want to redirect back to the form if there is an error (i.e. passwords don't match). How can I redirect back to the form and retain the contents of the other fields, so the user doesn't have to type it all over again? BTW I know I can do this with java on the actual form.php page, but was wondering if there was a PHP solution) Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 26, 2010 Share Posted May 26, 2010 The easiest method is if you put your form processing code and your form on one page. The easiest method, if you are using two different pages, would be to use $_SESSION variables so that the values are available between pages. Quote Link to comment Share on other sites More sharing options...
jeff5656 Posted May 26, 2010 Author Share Posted May 26, 2010 ok thanks. I use the same process page for different forms (just different if statements to direct to correct section), so I may need to use sessions. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 26, 2010 Share Posted May 26, 2010 along those same lines, you can also leave the processing page as is but just move the validation logic to the form page. Then when validation passes do an include of the processing page. If the validation logic is the same, then just do an include of that page in the appropriate place on the form pages as well. A modular design will save you a ton of time. Here is an example script <?php //Set some needed vars $errorMsg = ''; $name = ''; $email = ''; $age = ''; if(isset($_POST['name'])) { //Form was submitted, process data $name = trim($_POST['name']); $email = trim($_POST['email']); $age = trim($_POST['age']); //Validate data $errors = array(); if(empty($name)) { $errors[] = " - Name is required."; } if(empty($email)) { $errors[] = " - Email is required."; } //Check for errors if(count($errors)>0) { //There were errors, prepare error message $errorMsg = "The following errors occured:<br />"; $errorMsg .= implode("<br />\n", $errors); } else { //There were no errors, include processing page include("processData.php"); exit(); } } ?> <html> <body> <div style="color:red;"> <?php echo $errorMsg; ?> </div> <br /><br /> Enter your data <form name="test" action="" method="post"> <label for="name"><b>Name:</b></label> <input type="text" name="name" id="name" value="<?php echo $name; ?>" /> <br /> <label for="email"><b>Email:</b></label> <input type="text" name="email" id="email" value="<?php echo $email; ?>" /> <br /> <label for="age">Age:</label> <input type="text" name="age" id="age" value="<?php echo $age; ?>" /> <br /> * Required fields are in bold <br /><br /> <button type="submit">Submit</button> </form> </body> </html> 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.