Solved

Going back a page clears fields

Posted on 2015-02-01
11
51 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 83

Expert Comment

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

Author Comment

by:rivkamak
ID: 40583068
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 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
Technology Partners: 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 83

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 83

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 110

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 83

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CURL sending XML without spaces PHP 5 39
disable open new tab with right mouse click in <a> tag 3 21
ASP.NET data base connection 35 46
CSS for Popup in ASP.NET 4 22
Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime is disastrous for companies and can lead to major hits on a brand, reputation, an…
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

696 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