king arthur Posted January 10, 2007 Share Posted January 10, 2007 Is there a way to preserve the $_POST vars when doing a header redirect. I want to validate the inputs in a form and if they are valid, redirect to another website sending the same form data all with one click from the user. Quote Link to comment https://forums.phpfreaks.com/topic/33606-preserve-post-vars-in-header-redirect/ Share on other sites More sharing options...
taith Posted January 10, 2007 Share Posted January 10, 2007 not that i know of... but you can always[code]foreach($_POST as $k=>$v) echo '<input type=hidden name="'.$k.'" value="'.$v.'">';[/code]but you would need to resubmit that form... Quote Link to comment https://forums.phpfreaks.com/topic/33606-preserve-post-vars-in-header-redirect/#findComment-157449 Share on other sites More sharing options...
NickG21 Posted January 10, 2007 Share Posted January 10, 2007 i have been trying to do the same type of thing, except just have the stuff validated and reposted if it has to be, and i think as long as you have a session_start(); at the beginning of the site you are looking to post the final data to all of the variables should continue to be passed, even while using header. this is what i have been trying to use and it has been working alright[code]header("Refresh: 0; URL= http://PageForwardorBack.php");[/code]hope it works alright for you Quote Link to comment https://forums.phpfreaks.com/topic/33606-preserve-post-vars-in-header-redirect/#findComment-157453 Share on other sites More sharing options...
king arthur Posted January 10, 2007 Author Share Posted January 10, 2007 I'll try that one and let you know if it works, cheers. Quote Link to comment https://forums.phpfreaks.com/topic/33606-preserve-post-vars-in-header-redirect/#findComment-157583 Share on other sites More sharing options...
king arthur Posted January 11, 2007 Author Share Posted January 11, 2007 Having searched a little on Google it appears there isn't any proper way of doing this. I have seen it suggested that a 302 Temporary Redirect header should be sent first, but since I don't understand enough about what effect that has I think I will just have to send the vars using GET. I didn't want to do that as I didn't want them to appear in the address bar but it'll have to do for now. Quote Link to comment https://forums.phpfreaks.com/topic/33606-preserve-post-vars-in-header-redirect/#findComment-158459 Share on other sites More sharing options...
.josh Posted January 11, 2007 Share Posted January 11, 2007 in your validation script, start a session and create session variables for the form values. On your form page, also start a session and include the session variables as the value of your elements. If the session variables do not exist (as in, first time to the form page, not filled out, etc..) then it will simply show up as blank. Example (I even threw in a bit of custom error messages):form.php[code]<?php session_start(); // if there are errors... if ($_SESSION['error']) { // for each error... foreach($_SESSION['error'] as $error) { // echo out the error echo "$error <br />"; } // end for each error // unset the error array so that if user messes // up more than once, it won't keep stacking errors unset($_SESSION['error']); } // end if there are errors // if there is info if ($_SESSION['info']) { // for easier var handling... extract($_SESSION['info']); // no need for this session var anymore unset($_SESSION['info']); } // end if there is info// echo out the form, using the user's info as values// if there is none, it will simply be blankecho <<<FORMENT <form action = 'validate.php' method = 'post'> Name <input type = 'text' name = 'username' value='$username'> <br/> Something <input type = 'text' name = 'something' value='$something'> <br/> <input type = 'submit' value = 'submit'> </form>FORMENT;?>[/code]validate.php[code]<?php session_start(); // if vars are posted... if ($_POST) { // for easier variable handling... extract($_POST); } // end if posted vars /* if nothing was posted, or page was directly accessed all error messages will be generated, user will be kicked to form and all messages will be displayed */ // check if username filled out if (!isset($username) || trim($username) == '') { $error[] = "name not filled out"; } // end if name not filled out // check if something filled out if (!isset($something) || trim($something) == '') { $error[] = "something not filled out"; } // end if something not filled out // if an error was generated... if($error) { // create a session var to carry the posted vars back to form $_SESSION['info'] = $_POST; // create a session var for error messages to display on form $_SESSION['error'] = $error; // redirect back to form header('Location: form.php'); exit(); // end if errors } else { // form filled out successfully. Do something here like echo something. echo "thank you for filling everything out, or whatever."; } // end if form filled out successfully?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33606-preserve-post-vars-in-header-redirect/#findComment-158490 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.