Solved

Going back a page clears fields

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.php tree directory? 5 54
Form not operating correctly. 1 22
Regex PHP lines with newlines not being matched - all other match fine 4 21
how to use a function in heredoc 5 21
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

948 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

21 Experts available now in Live!

Get 1:1 Help Now