Solved

Going back a page clears fields

Posted on 2015-02-01
11
46 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
  • 5
  • 5
11 Comments
 
LVL 82

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 82

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
 

Author Comment

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

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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 82

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 108

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 82

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Read about why website design really matters in today's demanding market.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
This video teaches users how to migrate an existing Wordpress website to a new domain.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now