/    Sign up×
Community /Pin to ProfileBookmark

PHP Form Validation / Return for Correction

I need to create a form validation system where the user will be returned to the form with the valid data still in the form and the bad data marked for correction.

Can anyone point me to a good tutorial and/or examples for that sort of thing?

(I should add that I’m surprised at how little useful return I get on Google searches – Lots of javascript stuff but little or nothing that tells me how to return the form with the correct data still filled in. Perhaps I’m just using the wrong keywords, this seems like a pretty common requirement.)

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@scaiferwauthorJul 20.2010 — I think I have this figured out, and would welcome any comment.

The submit button on the form posts the data right back to the same page/script.

On loading, the page checks for a $_POST value to see if data has been posted.

If so, validation is carried out. If the data passes validation, it's posted to the database. Otherwise, the form is rendered again with the good data still showing, and inputs needing correction marked.

If no data has been posted, the empty form is rendered ready for input.

In pseudocode;

[CODE]Load page

if (data has been $_POSTed) {
if (data passes validation) {
post data database
} else {
Rebuild form with good data pre-filled and bad data fields marked
}
} else {
load empty form for initial input
}[/CODE]


Am I on the right track here?

Comments or suggestions welcome.
Copy linkTweet thisAlerts:
@NogDogJul 20.2010 — Yes, that is probably the most common pattern. As an added feature, if the form is successfully processed, it is common to use a "Location" header() to do a header redirect to a "success" landing page, in order to prevent the browser's refresh button from resubmitting the form data.

In order to prevent PHP notice-level warning for invalid array indexes, plus also allowing you to have the HTML in a separate file/function, you can use the following pattern to display a value:
[code=php]
<input type='text' name='sample' value='<?php
echo (isset($_POST['sample'])) ? htmlentities($_POST['sample'], ENT_QUOTES, 'UTF-8'), '';?>' />
[/code]


PS: You could, of course, build a little function that accepts the form name and does the isset() check and applies htmlentities(), so that you don't have to type all that out for each field.
×

Success!

Help @scaiferw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...