?
Solved

Going back a page clears fields

Posted on 2015-02-01
11
Medium Priority
?
60 Views
Last Modified: 2015-02-27
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?
0
Comment
Question by:rivkamak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40582876
Not without a link to look at to check it out.
0
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 40583089
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rivkamak
ID: 40587724
Is there a way I can change it, that it wont' use a session?
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40587767
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
 

Author Comment

by:rivkamak
ID: 40591500
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
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40613741
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
 

Author Comment

by:rivkamak
ID: 40614917
So for some reason, even the back button clears out the fields
That's what I don't understant
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40622335
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
 

Author Comment

by:rivkamak
ID: 40634731
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
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40634837
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

Featured Post

Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question