Link to home
Start Free TrialLog in
Avatar of BeerFizz
BeerFizz

asked on

form fields filled in on back button

I have an HTML form with about 20 fields.  

When the user presses submit, the form is precessed.  If there is incorrect information, I want the user to be able to press the back button og back to the original form but have data they input displayed again fvro modification.  Ie, I don't want them to have to enter everything again.
Avatar of VoteyDisciple
VoteyDisciple

That's mostly up to the browser; there isn't a lot you can do to prevent browsers intent on clearing the form from doing so.  It will probably help if the form is generated from what looks and acts like a static page (even if it's not); generating a form from a POST page, for example, can certainly encourage browsers to clear it out.


An alternative is to redraw the form yourself when there are errors, filling in the value="" attribute, and presumably adding in some error text ("You forgot this field, moron.").  This is a more traditional approach whenever you're working with server-side processing.
you can merge into your javascript code someting like:

var0 = document.form.element[0].value;                              // Here we stock form elements values into vars
var1 = document.form.element[1].value;
document.location = " ?var0="+var0+"&var1="+var1;        // Here we send the vars and values GET mode

and then if you  are using php in every element of the form you can fill with someting like

<input type="TEXT" name="var0" id="var0" value="<?=$_GET['var0']?>">
// In the value tag we catch the GET var that was received into the url called by javascript
Avatar of BeerFizz

ASKER

@limong

if I understand correctly, this data would then be passed back to the original form page as a parameter?

If this is the case, is that a security issue?

SOLUTION
Avatar of BrianGEFF719
BrianGEFF719
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
we could do it by post but in that case you need to let the form be submitted and specify into the form tag METHOD="POST",

you can also, submit the form to a hidden iframe located into the same page, and from there  validate the data and decide if you want to change the document location, or leave the form for corrections, in this examples the submit button is not an input type submit, you will need a regular button with a form.submit into the onclick event.

Ok,  I've been playing with this several different ways and have had no luck yet.

let me describe the process some more.

Once the form is completed and the submit button is pressed, control is passed to a PHP script which validates the data and regardless of good or bad, writes the data to a MYSQL data base.  Again, whether the data is valid or not control is passed to a 'response page' (php and html), which informs the user.  

The response page has no input fields or buttons, its simply informative.  In this page I also (with php) erase and destroy the session.

To go back to the original form the user has to press the back button.

So, when the user presses the  back button, I am not sure how to pass the information to the original form that the user has come from the response form and their data is in the database and can be retrieved and using php put into the "value".

ideas?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@js vaughn

Thank you for the reply.

Unfortunately, the response page is the last page.  In this page I destroy the session.

But, before destroying the session, I could hold the session id in a local variable.  If I was then able to "catch" the back button press/event, I could pass that in the url or as a post.   I am using https.  If I have the session id, I can pull all the data out of the db.

any ideas how I might do that?  javascript... etc

Thanks for all your help.
Only other way I could think of handling the problem is to use cookies instead of session variables.  Set you cookies at the same time the information is being sent to the database.  If the user hits the back button, the cookies will still exist and you can preload your form that way.  Of course if no cookies exist, just load a blank form.
you could get hidden fields into de response page and also a hidden form...., so send them againa to the first form without noone notice......
and from javascript do a form.submit();.... (to the "display= none" form) and have the values in the original form with value="$_POST['var1']"
@limong,

I was thinking over the weekend that this approach might be the only solution.  Now quite sure how to implement though.

Could you possibly create a mini example for me with a couple of fields?

Thanks in advance.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial