Going back a page clears fields

I have a form that when I hit submit to go the next page and then hit just the back button on my browser all my fields are cleared out.
What might be a setting that is causing that?
rivkamakAsked:
Who is Participating?
 
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
As far as I can remember, none of my PHP pages with forms that use sessions retain the values.  Usually that's what you want.  HTML pages seem to always keep the last values entered.
0
 
Dave BaldwinFixer of ProblemsCommented:
Not without a link to look at to check it out.
0
 
rivkamakAuthor Commented:
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
rivkamakAuthor Commented:
Is there a way I can change it, that it wont' use a session?
0
 
Dave BaldwinFixer of ProblemsCommented:
I don't know why you want to do that, most people want the forms erased on 'back'  I don't see anything on that page that requires PHP so you might be able to just make it an HTML page and then it should remember the last entries when you go 'back'.  There are a bunch of extra </input> tags that shouldn't be there, you might want to remove them.
0
 
rivkamakAuthor Commented:
the problem i am encountering if my form accepts credit card.
after they hit submit, it goes to page 2 and tries their credit card.
if it's invalid, it goes back 1 page and asks them to try again.
but poor them, all their other fields cleared out and they have to start again from scratch.
how can i avoid this?
0
 
Dave BaldwinFixer of ProblemsCommented:
I just re-read your last post.  I suggest that you put up a message and ask them to click on 'Back' instead of automatically loading the 1st page.  I believe your method is making it load as new page.  Clicking on the back button will normally restore the previous page in it's last state.
0
 
rivkamakAuthor Commented:
So for some reason, even the back button clears out the fields
That's what I don't understant
0
 
Ray PaseurCommented:
A good "step one" thing would be to fix the document markup so you're not accidentally triggering unwanted browser behavior.
http://validator.w3.org/check?uri=http%3A%2F%2Fdenverkollel.org%2Fregistration%2Fdinner2015.php&charset=%28detect+automatically%29&doctype=Inline&group=0

This shows a PHP way of remembering the form inputs and highlighting the errors.  A more modern and user-friendly approach today might include AngularJS or a similar JavaScript validation of the form.
<?php // demo/form_highlight_errors.php

/**
 * Demonstrate how to highlight errors in form input, and remember valid inputs
 * Client is asked to put in a value
 * If the value fails our test we show an error message
 * We put a marker next to the input control on the form
 * We turn the form border red
 * SEE http://www.w3schools.com/CSS/pr_class_visibility.asp
 */
error_reporting(E_ALL);


// THESE STYLE ELEMENTS ARE SET FOR THE SCRIPT INITIALIZATION
$error_abc = $error_xyz = $error_any = 'hidden';
$boxer_abc = $boxer_xyz              = 'black';


// CAPTURE AND NORMALIZE THE POST VARIABLES - ADD YOUR OWN SANITY CHECKS HERE
$abc = (isset($_POST["abc"])) ? trim(strtoupper($_POST["abc"])) : NULL;
$xyz = (isset($_POST["xyz"])) ? trim(strtoupper($_POST["xyz"])) : NULL;


// IF ANYTHING WAS POSTED, VALIDATE IT
if (!empty($_POST))
{
    // VALIDATE THE 'abc' FIELD
    if ($abc != 'ABC')
    {
        // VALIDATION FAILED
        $error_abc = $error_any = 'visible';
        $boxer_abc = 'red';

        // BECAUSE THIS FAILED VALIDATION, REMOVE IT FROM THE FORM
        $abc       = NULL;
    }

    // VALIDATE THE 'xyz' FIELD
    if ($xyz != 'XYZ')
    {
        // VALIDATION FAILED
        $error_xyz = $error_any = 'visible';
        $boxer_xyz = 'red';

        // BECAUSE THIS FAILED VALIDATION, REMOVE IT FROM THE FORM
        $xyz       = NULL;
    }

    // DO WE HAVE INPUT FREE FROM ANY ERRORS?
    if ($error_any != 'visible')
    {
        echo "CONGRATULATIONS";
        die();
    }

    // OTHERWISE... OOPS - WE HAVE ERRORS AND MUST SHOW THE FORM AGAIN
}

// IF NOTHING WAS POSTED, OR IF THERE ARE ERRORS, WE NEED NEW CLIENT INPUT
$form = <<<ENDFORM
<style type="text/css" media="all">
.error_any { visibility:$error_any; }
.error_abc { visibility:$error_abc; }
.error_xyz { visibility:$error_xyz; }
</style>
<pre>
<form method="post">
<span class="error_any">PLEASE CORRECT THE FOLLOWING ERRORS</span>
<span class="error_abc">YOU MUST ENTER 'abc' IN THIS FIELD</span>
PLEASE ENTER "ABC" HERE: <input style="border-color:$boxer_abc;" name="abc" value="$abc" />
<span class="error_xyz">YOU MUST ENTER 'xyz' IN THIS FIELD</span>
PLEASE ENTER "XYZ" HERE: <input style="border-color:$boxer_xyz;" name="xyz" value="$xyz" />
<input type="submit" />
</form>
ENDFORM;

// WRITE THE FORM WITH THE APPROPRIATE CSS STYLES ON THE ERROR MESSAGE FIELDS
echo $form;

Open in new window

0
 
rivkamakAuthor Commented:
Let's say I want to set up the whole form in session variables.
Is there a loop I can run to automatically set up all the form variables into their own session variables and then I can refer them back?
0
 
Dave BaldwinFixer of ProblemsCommented:
Yes.  I've done that a number of times.  It's a lot of work but if you need persistent data across a number of pages, that's a way to do it.  You will need to use 'isset' to detect whether there is data available.  http://php.net/manual/en/function.isset.php
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.