zfred09 Posted December 23, 2006 Share Posted December 23, 2006 Ok I have multiple forms (for registration) and if someone were to fill out all the information but have an error in what they entered, how do I keep the form values they entered in the form after they get an error message? Basically so they don't have to re-type all their information after one little error. The script goes from the registration page to a process page, and if there is an error it sends the user back to the registration page with the error message. Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/ Share on other sites More sharing options...
fert Posted December 23, 2006 Share Posted December 23, 2006 you would use headers to cache the form. Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/#findComment-147015 Share on other sites More sharing options...
Jessica Posted December 23, 2006 Share Posted December 23, 2006 Save the values to the session. On the processing page, create an array, say $form. Then say $form['name'] = $_POST['name']; //Make sure to actually sanitize your data.Before redirecting back to the form with the error, do $_SESSION['form'] = $form;On the form page get the form out of the session: $form = $_SESSION['form']; In each of the inputs add value="<?=$form['name']?>"etc. Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/#findComment-147016 Share on other sites More sharing options...
zfred09 Posted December 23, 2006 Author Share Posted December 23, 2006 Thanks for the help so far, I will try to use it to figure something out and will post again if I have problems. Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/#findComment-147021 Share on other sites More sharing options...
Jessica Posted December 23, 2006 Share Posted December 23, 2006 You can also use javascript to validate as best as possible before they submit, but always validate server side TOO. Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/#findComment-147027 Share on other sites More sharing options...
zfred09 Posted December 23, 2006 Author Share Posted December 23, 2006 What does validating server side mean? Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/#findComment-147035 Share on other sites More sharing options...
Jessica Posted December 23, 2006 Share Posted December 23, 2006 Javascript is client side - PHP is server side. Javascript validation can be bypassed - PHP cannot (as long as you do it correctly)To clarify: Javascript runs in your visitor's browser, meaning they have control over it. Your PHP runs on your server. Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/#findComment-147036 Share on other sites More sharing options...
zfred09 Posted December 23, 2006 Author Share Posted December 23, 2006 Ah I get it, ya all my validation is done with php. Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/#findComment-147038 Share on other sites More sharing options...
kamasheto Posted December 23, 2006 Share Posted December 23, 2006 [quote author=jesirose link=topic=119769.msg490793#msg490793 date=1166907185]Save the values to the session. On the processing page, create an array, say $form. Then say $form['name'] = $_POST['name']; //Make sure to actually sanitize your data.Before redirecting back to the form with the error, do $_SESSION['form'] = $form;On the form page get the form out of the session: $form = $_SESSION['form']; In each of the inputs add value="<?=$form['name']?>"etc.[/quote]Pheww.. blame me for not having a clue what that isanyway, how I'd suggest you do it is to point your action to point your form to the file you're submitting from; here's how[code]<?phpif(isset($_POST['submit'])){ $error = ""; if(!strlen($_POST['username'])) { $error .= "You need to enter a username<br />"; } if(!strlen($_POST['password'])) { $error .= "You need to enter a password<br />"; } if(strlen($error)) { showForm($error); } else { // process our input here }} else { showForm();}function showForm($e=""){ if(strlen($e)) { $e = "<font class='error'>".$e."</font>"; } else { $e = ""; $_POST = NULL; } print "<html> <head> <title>My Test Form - kamasheto</title> <style>body { font-size:14px;font-family:Verdana, Sans } .error { font-variant: small-caps; }</style> </head> <body> {$e} <form action='?' method='post'> <input type='hidden' name='submit'> Username: <input type='text' name='username' value='{$_POST['username']}'><br /> Password: <input type='password' name='password' value='{$_POST['password']}'><br /> <input type='submit' value='login'> </form> </body> </html>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/#findComment-147039 Share on other sites More sharing options...
Jessica Posted December 23, 2006 Share Posted December 23, 2006 I would recommend having a separate processing file - although I can't think of a way to explain why :( Also, make sure to sanitize your user's data before printing it back to the screen, or you open yourself up to XSS attacks. Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/#findComment-147043 Share on other sites More sharing options...
kamasheto Posted December 23, 2006 Share Posted December 23, 2006 I was just telling him how, no security involved.The way I'd normally do it is pass my $_POST array to a function that does all those fancy little checks and replacements before I use them anywhere, whether it's something as plain as this to print them back to the user or as severe as inserting them in a database. Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/#findComment-147044 Share on other sites More sharing options...
zfred09 Posted December 23, 2006 Author Share Posted December 23, 2006 What does sanitizing mean? Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/#findComment-147048 Share on other sites More sharing options...
Jessica Posted December 23, 2006 Share Posted December 23, 2006 It means making sure the user didn't enter any data you don't want. You don't want them to inject SQL, HMTL or Javascript. Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/#findComment-147051 Share on other sites More sharing options...
zfred09 Posted December 23, 2006 Author Share Posted December 23, 2006 How do you go about doing that? And injecting it into where, the form inputs? Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/#findComment-147054 Share on other sites More sharing options...
Jessica Posted December 23, 2006 Share Posted December 23, 2006 There are plenty of resources about Web Security and sanitizing data. I'm sure you can find some if you search for SQL injection, XSS, etc. Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/#findComment-147055 Share on other sites More sharing options...
zfred09 Posted December 23, 2006 Author Share Posted December 23, 2006 Alright thanks for the tip. Quote Link to comment https://forums.phpfreaks.com/topic/31718-solved-keeping-form-values-after-error-message/#findComment-147058 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.