dbk Posted June 3, 2010 Share Posted June 3, 2010 I have at quite large form at one page, and there fore the validation is placed in the page that handles the form. Now.. if the validation sendt an error I have made a new page with the errormessage where from you can choose to correct the form data or cancel. If the user want to correct the form data I sent him back with '<a href="javascript:javascript:history.go(-1)">' . Is this a bad way? and are there a smarter way? Quote Link to comment https://forums.phpfreaks.com/topic/203784-avoid-to-rewrite-form-if-validation-sendt-error/ Share on other sites More sharing options...
Psycho Posted June 3, 2010 Share Posted June 3, 2010 The script which creates the form should also include the validation logic and the form should post to itself. Then if validation fails you can repopulate the form with the POSTed values. If validation passes you can then include the script wich processes the data. Of course you can separate any of the above logic into individual files. If you have a large form then you will probably want to have separate files for the form and validation as well. Here is an example of a possible file structure: contact.php (This page determines if the form data has been posted. If not, the form script is included. If post data has been submitetd (and fails) then the form script is included (and previous values populated). If validation passes, then the processing page is included) validate.php this would contain the validation logic form.php this would contain just the form with variables to populate previous data if available process.php This would be called when validation passes an would process the actual data thankyou.php This is the page to display after processing has been complete. If you call the page by using a header() to redirect, then the POST data will be cleared. This prevents data from beng submitted twice if the user hits the refresh button Quote Link to comment https://forums.phpfreaks.com/topic/203784-avoid-to-rewrite-form-if-validation-sendt-error/#findComment-1067297 Share on other sites More sharing options...
dbk Posted June 3, 2010 Author Share Posted June 3, 2010 Yes.. Thank You!! That's ofcourse the way to do it! (Why didn't I think of the include if validation passes ) Perfect! Quote Link to comment https://forums.phpfreaks.com/topic/203784-avoid-to-rewrite-form-if-validation-sendt-error/#findComment-1067302 Share on other sites More sharing options...
Psycho Posted June 3, 2010 Share Posted June 3, 2010 Here are some example scripts to give you an idea: contact.php (main script called by user) <?php if(isset($_POST['name'])) { //Form was submitted, pre-process data $name = trim($_POST['name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); $message = trim($_POST['message']); //include validation script include('validate.php'); if($valid) { //Submission was valid include processing script include('process.php'); exit(); } //Validation failed, form will be displayed } //No post data or validation failed, include form include('form.php'); ?> form.php <html> <body> <div style="color:#ff0000;"><?php echo $error_message; ?></div> Contact us:<br /> <form name="contact" action="" method="POST"> <b><label for="message">Name: </label></b> <input type="text" name="name" id="name" value="<?php echo $name; ?>" /><br /> <b><label for="message">Email: </label></b> <input type="text" name="email" id="email" value="<?php echo $email; ?>" /><br /> <label for="message">Phone: </label> <input type="text" name="phone" id="name" value="<?php echo $phone; ?>" /><br /> <b><label for="message">Message</label></b> <textarea name="message"><?php echo $message; ?></textarea> <br /><br /> <button type="submit">Submit</button> </form> </body> </html> validate.php <?php $errors = array(); //Validate the data if(empty($name)) { $errors[] = " - Name is required."; } if(empty($email)) { $errors[] = " - Email is required."; } if(empty($message)) { $errors[] = " - Message is required."; } if(count($errors)==0) { $valid = true; } else { $error_message = "<b>The following errors occured:</b><br>\n"; $error_message .= implode("<br />\n", $errors); $valid = false; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/203784-avoid-to-rewrite-form-if-validation-sendt-error/#findComment-1067307 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.