PHP Form Handing

July 10, 2007 by hejian

1. Use $_SERVER['PHP_SELF'] as a form action

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>”>

2. Test for form submission with a hidden element

if (array_key_exists('_submit_check', $_POST)) {
/* ... do something with the form parameters ... */
}
...
<input type="hidden" name="_submit_check" value="1"/>

3. Divide your form handling into three parts: showing, validating, and processing

if (array_key_exists('_submit_check',$_POST)) {
// If validate_form() returns errors, pass them to show_form()
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
// The submitted data is valid, so process it
process_form();
}
} else {
// The form wasn't submitted, so display
show_form();
}

Leave a Reply

You must be logged in to post a comment.

Wordpress template made by HeJian