Jump to content

Preserve post vars in header redirect


king arthur

Recommended Posts

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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 blank
echo <<<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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.