Jump to content

Recommended Posts

Hi, I am looking for some help with a php scenario and haven't found a complete solution yet.  I have a form with a textarea that gets submitted and then shows a preview of what the user typed, asking them if they want to post this or go back and edit it.

 

The thing I don't like, is if the user is on the "preview" page and clicks refresh or uses the browsers back button and gets to this "preview" page, they get the message about "the post data needs to be resent to view this page OK - CANCEL".  I don't want this to happen, so I was going to use a header("Location: ....") to redirect from the page where the post data is received to a new page that gets the data from the query string (url) and displays that.  This worked for short posts. but if the user typed a long message in the text area, the header redirect fails (I urlencoded the data, so that's not the problem it IS the length). So I'm wondering if there is another way.

 

I have read about passing the data in session variables and yes, that does work, but if the user goes through multiple posts and then goes back to the first form, it will have display the data from the second form...Unless I use a different variable for every time a user encounters a form (which would waste and eventually run out of memory). Is there any way to have the page only use the dynamic session data the first time and then somehow display it as a static page from then on? Or some kind of cache setting I could change, so that it doesn't get this data from the session after the first time?  Any help with this is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/89988-passing-post-data-and-redirecting/
Share on other sites

Yeah, URLs actually have a max length, I forget the max, but there is one.

 

What you wanna do is store the variables in the session. Here is an example:

 

form.php

<?php
  //Start the session
  session_start();

  //Catch a POST
  if($_SERVER['REQUEST_METHOD'] == 'POST'){
    if($_POST['preview']){
      $_SESSION['formVars'] = $_POST;
      header('Location: preview.php');
      exit;
    }
    //Looks like we are submitting
    $_SESSION['formVars'] = $_POST;
    header('Location: submit.php');
    exit;
  }

  //If stuff is in the session, get it, otherwise, empty array
  $vars = is_array($_SESSION['formVars']) ? $_SESSION['formVars'] : array();
?>
<html>
  <body>
    <form method="post">
      <textarea name="mytext"><?php echo htmlspecialchars($vars['mytext']); ?></textarea><br />
      <input type="submit" name="submit" value="Submit" />
      <input type="submit" name="preview" value="Preview" />
    </form>
  </body>
</html>

 

preview.php

<?php
  //Start the session
  session_start();

  //Check for no data
  if(!count($_SESSION['formVars'])){
    header('Location: form.php');
    exit;
  }
  $vars = $_SESSION['formVars'];
?>
do up your preview here, using values from $vars

<input type="button" value="Keep Editing" onclick="window.location.href='form.php';" />
<input type="button" value="Submit" onclick="window.location.href='submit.php';" />

 

submit.php

<?php
  //Start the session
  session_start();

  //Check for no data
  if(!count($_SESSION['formVars'])){
    header('Location: form.php');
    exit;
  }
  $vars = $_SESSION['formVars'];

  //Do what you need to do (database, email, etc);

  //On Success
  $_SESSION['formVars'] = null;
  print "Thanks";
?>

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.