binarylime Posted February 13, 2008 Share Posted February 13, 2008 Alright guys, I'm having a problem with forms. When a user registers, the form is processed by a PHP script. If there is a problem with the form (passwords don't match, username already taken...), the user is sent back to the form with an error message using require_once('registerform.php'). When users are sent back to the form, all their original data is gone. It's annoying for users if their passwords don't match, and they are thrown back to the form and have to fill everything in from scratch. How can i retain the users data in the form fields? Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/90888-retain-form-field-information-after-php-verification/ Share on other sites More sharing options...
pdkv2 Posted February 13, 2008 Share Posted February 13, 2008 Are you using any template parser ??? if not try to use the template parsers like smarty ! this will solve the problem Link to comment https://forums.phpfreaks.com/topic/90888-retain-form-field-information-after-php-verification/#findComment-465784 Share on other sites More sharing options...
binarylime Posted February 13, 2008 Author Share Posted February 13, 2008 No, i just have a standard form which is received my my php processing script which uses several if statements as follows: if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['confirmpass']) || empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['email']) || empty($_POST['institutename'])) { $warningmessage = "One or more fields missing."; require_once('registerform.php'); exit; } Link to comment https://forums.phpfreaks.com/topic/90888-retain-form-field-information-after-php-verification/#findComment-465788 Share on other sites More sharing options...
pdkv2 Posted February 13, 2008 Share Posted February 13, 2008 No, i just have a standard form which is received my my php processing script which uses several if statements as follows: if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['confirmpass']) || empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['email']) || empty($_POST['institutename'])) { $warningmessage = "One or more fields missing."; require_once('registerform.php'); exit; } Do one thing here like store the Post data in the variables e.g. $username=$_POST['username']; and so on and use these variables in your "registerform.php" to assign to the appropriate fields e.g. <input type='text' name='username' value='<?php echo $username; ?>'> etc Cheers ! Link to comment https://forums.phpfreaks.com/topic/90888-retain-form-field-information-after-php-verification/#findComment-465793 Share on other sites More sharing options...
binarylime Posted February 13, 2008 Author Share Posted February 13, 2008 Thanks pdkv2! Sorted! Appreciate the really quick help! Link to comment https://forums.phpfreaks.com/topic/90888-retain-form-field-information-after-php-verification/#findComment-465804 Share on other sites More sharing options...
haku Posted February 13, 2008 Share Posted February 13, 2008 pdkv2's solution was entirely fine, and will work great. But for the sake of the cleanest output possible, I personally prefer to write it this way: <input type="text" name="username"<?php if(isset($_POST['username'])) { echo ' value="{$_POST['username']}"'; ?> /> The reasoning behind this is that in the the original explanation, if the form has not been submitted, the tag will look like this: <input type=2text" name="username value="" /> Whereas if done my way, the 'value' is only included if necessary. Its really not that important, and will work either way, but for the sake of clean code... Link to comment https://forums.phpfreaks.com/topic/90888-retain-form-field-information-after-php-verification/#findComment-465837 Share on other sites More sharing options...
pdkv2 Posted February 13, 2008 Share Posted February 13, 2008 hi haku, i really appreciate your suggestion, btwn how r you ? u was suggested me this in some other post also ! really thanks for this, this code passes the XHTML Standers if i am not mistaking Thanks Again Cheers ! Link to comment https://forums.phpfreaks.com/topic/90888-retain-form-field-information-after-php-verification/#findComment-465842 Share on other sites More sharing options...
haku Posted February 13, 2008 Share Posted February 13, 2008 No worries! Ya, I think its not valid XHTML if there is no value included. For most of the posters on the site, thats not a big issue, but valid semantic XHTML (or at least somewhat semantic) is something I put a lot of effort into writing. Link to comment https://forums.phpfreaks.com/topic/90888-retain-form-field-information-after-php-verification/#findComment-465846 Share on other sites More sharing options...
GameYin Posted February 13, 2008 Share Posted February 13, 2008 Try to escape the $_POST variables. Link to comment https://forums.phpfreaks.com/topic/90888-retain-form-field-information-after-php-verification/#findComment-465907 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.