siabanie Posted July 14, 2011 Share Posted July 14, 2011 Hi all, I would like to know how we can retrieve all the values / data that we filled in the form so when we press the button "back" it is not disappear? I have three pages which are, checkForm.php, processForm.php and thankyouForm.php Where I should put the SESSION code? Here is the snippets of my checkForm.php: .. .. <table width="100%" border="0" cellspacing="5" cellpadding="0"> <tr> <td width="20%">Name</td> <td width="80%"><input name="name" type="text" id="name" size="30" /></td> </tr> <tr> <td>Tel</td> <td><input name="tel" type="text" id="tel" size="30" /></td> </tr> <tr> <td>E-mail</td> <td><input name="email" type="text" id="email" size="30" /></td> </tr> </table> .... Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/241963-using-session/ Share on other sites More sharing options...
Adam Posted July 14, 2011 Share Posted July 14, 2011 You shouldn't use a session for this. Assuming the form isn't shown on the page with the back button, just include all the form data as hidden inputs. When the data is then submitted back to the form, you just need something like this on each input: <input type="text" name="example" value="<?php if (isset($_POST['example'])) echo htmlspecialchars($_POST['example']); ?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/241963-using-session/#findComment-1242612 Share on other sites More sharing options...
siabanie Posted July 14, 2011 Author Share Posted July 14, 2011 Hi Adam, Thanks for your feedback, I have tried as you suggested as: .. .. <tr> <td width="20%">Name</td> <td width="80%"><input name="name" value="<?php if (isset($_POST['name'])) echo htmlspecialchars($_POST['name']); ?>" type="text" id="name" size="30" /></td> </tr> <tr> .. .. I filled the form etc then submit it, when I press back button or click the back link (I use Javascript script to go back e.g: Please go <a href='javascript:history.go(-1)'>back</a> and try again) - The field name is empty? Am I missing something here? -- And yes I have put the session_start(); at the top of every page. Any idea what I did wrong? Quote Link to comment https://forums.phpfreaks.com/topic/241963-using-session/#findComment-1242623 Share on other sites More sharing options...
ZulfadlyAshBurn Posted July 14, 2011 Share Posted July 14, 2011 you should use javascript to validate your form then ajax submit. Quote Link to comment https://forums.phpfreaks.com/topic/241963-using-session/#findComment-1242713 Share on other sites More sharing options...
siabanie Posted July 14, 2011 Author Share Posted July 14, 2011 you should use javascript to validate your form then ajax submit. Hi ZulfadlyAshBurn, I am using JavaScript to validate the form e;g: $(document).ready(function(){ $('#btn_submit').click(function() { var error_num = 0; var error_mesg = ""; //alert("in"); $('[name=name]').parent().removeClass('error'); if($('[name=name]').val()==""){ error_num++; error_mesg += "Please Enter Your Name。\n"; $('[name=name]').parent().addClass('error'); } $('[name=captcha_code]').parent().removeClass('error'); if($('[name=captcha_code]').val()==""){ error_num++; error_mesg += "Please enter the image verification\n"; $('[name=captcha_code]').parent().addClass('error'); } if(error_num>0){ alert(error_mesg); }else if(error_num==0){ $('#present_form').submit(); } return false; }); }); </script> But the problem is; whenever I press "back" button all the values that I have just filled in gone then I have to fill it again which is annoying -- Plus How I can validate the securimage CAPTCHA (http://www.phpcaptcha.org/) using AJAX? I cannot seem able to show the error message on the same page but able to do it for the next page (If user clicked submit form and entered a wrong code) Quote Link to comment https://forums.phpfreaks.com/topic/241963-using-session/#findComment-1242716 Share on other sites More sharing options...
Adam Posted July 14, 2011 Share Posted July 14, 2011 [...] include all the form data as hidden inputs. When the data is then submitted back [...] Let's say the form is in form.php, and you process it in a file called process.php. When the data is submitted to process.php from form.php, you should include all the form data you need to pass back, within another form in process.php but as hidden inputs. The user won't know see the inputs, but it's effectively like them filling it all in again. Clicking back should submit said form, effectively posting the data back to form.php, but from process.php. Then the code I provided before will work. Quote Link to comment https://forums.phpfreaks.com/topic/241963-using-session/#findComment-1242805 Share on other sites More sharing options...
siabanie Posted July 14, 2011 Author Share Posted July 14, 2011 Thanks Adam, it is quite difficult to imagine without an example...but thanks. One question though; do you know how AJAX works in term of check validation (CAPTCHA)? Quote Link to comment https://forums.phpfreaks.com/topic/241963-using-session/#findComment-1242898 Share on other sites More sharing options...
djlee Posted July 14, 2011 Share Posted July 14, 2011 i see the problem (and probably confusion). The back button your referring to is the browser back button (or an equivalent hijacking of its function via javascript anchor). What your trying to accomplish is difficult, simply because you have to contend with browser caching and everything else. Plus you cant post back data. You have 2 options. 1. use the session. Its generally frowned upon, but sometimes it's the only way 2. create your own back button link that does as adam suggested basically <form> <div style='display:hidden'> <!-- put all your inputs here with their names the same as the real form and the value attribute set to $_POST[input_name]--> </div> <input type='submit' value='Go Back' /> </form> That would basically cause you to go back to the form page, but it will post back all the form data so you can prepopulate the form again. However javascript validation should cover most error's anyway. So if you implement ood JS validation, the odd user getting caught out via the backend script shouldn't be an issue Quote Link to comment https://forums.phpfreaks.com/topic/241963-using-session/#findComment-1242902 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.