Jump to content

repopulate form fields


Ninjakreborn

Recommended Posts

is this the correct way to repopulate a form field. meaning if it is submitted and validates wrong it leaves in what they already typed, and if they hit refresh is it still suppose to stay there, because right now when I use the code, it doesn't stay where it is supposed to stay when I press refresh.
[code]
<input tabindex="1" name="firstname" id="firstname" type="text" maxlength="30" value="<?php echo $_POST["firstname"]; ?>" />
[/code]
I thought that for instance, when someone typed in firstname here, it was recorded, adn then if it was refreshed, or changed screens, or flickered in the server and back out to validate that it would keep that field full of what they had previously typed. Or is this somethign that will work, just not when someone hits the refresh button, if not should I try to come up with a way to keep it from clearing when the refresh button is pressed.
Link to comment
Share on other sites

This is normal behavior with refresh. Refrsh clears any POST/GET data currently being transferred to the server/client. Thefore your POST'd data is not available any more. If you want the data that is currently in the form fields then you might want to save any POST'd data into a cookie/session and each time the page is requested put in the saved data into the form fields.
Link to comment
Share on other sites

Ok so then let me ask this.
If I leave that code I showed you above in all my values for text fields, when someone clicks submit, it validates it on the other page, and comes up with the error messages, when they hit the back button will that code keep it in the places they already filled out. The other question was, I have it sent to another page, where it's processing it and emailing it to me. I want to validate it with php too, should i just write up some functions on the form page, and if I do will it automatically run them when I click submit, or do I need to just do it on the other page, what I need is for it to run through all the validation functions, return all the errors, which I can do, but then if it doesn't have any email it, I am worried the ground of validation functions, and the mail functions are going to conflict with each other somehow, any advice. The main thing I need to know is about the repopulation, will that string of code show work like that.
Link to comment
Share on other sites

The proper way to do it is to have your validation code on the same page as the form. When the user submits the form it refers back to the same page and if the form results are invalid, you show the form again and the values are already there in the POST/GET. But you do all the validation before any HTML so if the validation is correct, you send the user onto a "thank you" page with the header() call. You can do the mailing after the validation or on the thank you page, it doesn't matter which one.
Link to comment
Share on other sites

The thing I don't understand is, now that I worked with it, I have the answers completely, it validates and processes on an external page. But I didn't need to do anything with repopulation. When they hit the back button it automatically still has the info in the slots, that is confusing.
Link to comment
Share on other sites

That's correct in most cases, but why should your user have to hit the back button??? You should be doing that for them.

And at least for IE, you'll often get "Page Expired" errors when you try to go back to a form like that.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.