Jump to content

Form values lost when back (or javascript history -1) is used.


Recommended Posts

My normal method of form handling is a file like contact.php with a handler like contacthandler.php. In contacthandler.php there would be a validation routine. If the form does not validate, I offer a javascript go-1 and the user can make corrections.

 

My problem is with a script written by someone else. (I think it's related to the fact that it uses sessions.) I have added my form and everything works except that when you return to the previous page the form is blank.  Quite frustrating for a long form.  I know I can re-call the page with a submit button and hidden fields then fill in all the fields with

value="<?php echo($whatever);?>"

But I'd rather find a way to keep those values in history.

 

Can anyone give me a clue, or if it can't be done explain why?

I posted in another forum and did not get an answer.

 

Tec.

Here's what I do (instead of using js history -1)....of course this is very basic & there's really no input security in this script that I'm about to share.

<?php
## FORM
function form(){
  foreach ($_POST as $key => $value){ // get form values (if it has been filled out already)
    $$key = trim($value); // sets input "names" as $email, $subject...etc
  }
  //echo the form
  echo'
  <form action="contact.php" method="post">
  <p>* = required</p>
  <p>* Email: <input type="text" name="email" size="38" value="'.$email.'" /><br />
  Subject: <input type="text" name="subject" size="38" value="'.$subject.'" /><br />
  * Message: <textarea name="message" class="medlength" rows="5" cols="40">'.$message.'</textarea><br />
  <input type="submit" name="submit" value="Submit" /></p>
  </form>';
}
## END FORM

if (!isset($_POST['submit'])){ //if form has NOT been submitted
  form(); // show the form!
}
else {
  foreach ($_POST as $key => $value){ // get form values
    $$key = strlen($key) > 1 ? trim($value) : null; // check that values are not empty, if so, make them NULL
  }
  if($email && $message){ // if $email & $message are not null....GO!
echo "SENT!<br />
Email: $email<br />
Subject: $subject<br />
Message: $message
";
  }
  else{ // if $email or $message are empty....HALT!
echo "ERROR! - Please fill in ALL requied fields";
form(); // show the form again....any typed information will still be there.
  }
}
?>

Thanks. That is a more elegant way of doing basically what I was trying to avoid, because my way was not elegant. I will consider using it that way.

 

Edit:

$$key = strlen($key) > 1 ? trim($value) : null;

I am not accustom to the shorthand if-then. Can you help me out with this a bit? 

If I am reading it correctly I wonder: Why are you checking the length of $key?

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.