Love2c0de Posted August 21, 2013 Share Posted August 21, 2013 Good morning, I have a simple 1 page website which I'm working on and I'd like to be able to fill the input/textarea field(s) with any $_POST data should they get re-directed back to the form. I've tried adding: <input type="text" value="<?php if(isset($_POST['name'])){ print($_POST['name']); } ?>" /> but it doesn't seem to be working. I'm using header("Location: ../index.php#contact_form"); within my action script if any of the required fields are empty. Here is my full code: <form id="contact_form" method="post" action="core/process_form.php"> <p><label for="name">Name:</label><input type="text" name="name" id="name" />* <span class="error">Cannot be empty!</span></p> <p><label for="number">Number:</label><input type="text" name="number" id="number" /><span>( Optional )</span></p> <p><label for="email">Email:</label><input type="text" name="email" id="email" />* <span class="error">Cannot be empty!</span></p> <p class="textarea"><label for="comment">Comment:</label><textarea name="comment" id="comment" cols="35" rows="10"></textarea></p> <p class="input"><input type="submit" value="Send" onclick="return process_form()" /><input type="reset" value="Clear" /></p> </form> Action Script //get post data echo "<pre>"; print_r($_POST); echo "</pre>"; foreach($_POST as $k => &$v) { $v = trim($v); } if($_POST['name'] == "" || $_POST['email'] == "") { header("Location: ../index.php#contact_form"); die(); } Thank you in advance for any info! Regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/281418-remembering-_post-data/ Share on other sites More sharing options...
requinix Posted August 21, 2013 Share Posted August 21, 2013 (edited) POST data is not saved during a redirect. You have to display the form immediately if you want to make it "sticky" (as it's commonly called). No redirections. [edit] Yeah, or you can take cheap ways out and, eg, stuff the data in the session, but I hate that. Edited August 21, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/281418-remembering-_post-data/#findComment-1446057 Share on other sites More sharing options...
Love2c0de Posted August 21, 2013 Author Share Posted August 21, 2013 Thank you requinix, Is it bad practice to use the session for the data or is it something which is accepted to use? When you say 'sticky', if I was to do the form processing within the same file, could I create that affect and would it be a more efficient way to do it? Thank for reply. Kind regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/281418-remembering-_post-data/#findComment-1446058 Share on other sites More sharing options...
requinix Posted August 21, 2013 Share Posted August 21, 2013 Is it bad practice to use the session for the data or is it something which is accepted to use?It's as acceptable as it is easy to do, that's really what it comes down to. Mind you the user can't be browsing more than one of these forms at once, and there'll be a magical behavior where the form is filled out for them when they might not expect it, but most of the time that's okay. (There are workarounds but they have issues too.) Essentially you pick a place in $_SESSION and stuff $_POST in there. $_SESSION["that one form over there"] = $_POST;Then your form looks to the session to pre-populate the form before using its defaults. It's called "sticky" because the values "stick" between visits to the form. When you say 'sticky', if I was to do the form processing within the same file, could I create that affect and would it be a more efficient way to do it?That's the way I prefer to do it. Means you don't pollute the session with what is really just temporary information. The drawback is that users get one of those "resubmit form data" warnings if the come back to the form and simply try to refresh the page, but that's exactly what the warning is supposed to be for anyways. Quote Link to comment https://forums.phpfreaks.com/topic/281418-remembering-_post-data/#findComment-1446064 Share on other sites More sharing options...
Love2c0de Posted August 21, 2013 Author Share Posted August 21, 2013 That's great, thank you for your advice. I think I will process the form data within the template page so that I am not forced to redirect the user and use sessions. Kind regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/281418-remembering-_post-data/#findComment-1446065 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.