Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

php form display function

0 views
Skip to first unread message

William Gill

unread,
Apr 7, 2008, 1:49:24 PM4/7/08
to
I hope I can state this clearly enough.

I am redoing some of my html(php) forms. I'm trying to break things
into three functions on a self processing php page:
1) show the form (and populate fields if there were errors)
2) validate form data
3) process form data

2 and 3 are no problem, but 1 is. The page with the form displayed
should validate as html 4.01 strict (actually all iterations of the page
need to validate). The actual page is a mix of php and html (popping in
and out of php w/ '<?PHP' and '?>' as needed, so that I can see what
the page layout will be. To validate I have been serving page.php
saving it as page.html and validating that. I'm sure there is a better
way to do this.

To keep the code logic and markup 'separate' and easier to read, I have
been using include files of valid html (i.e. a div containing the
form,with my pop-in's of php, or a div containing the 'thank you, we'll
get back to you'). like this:

<?php
if (array_key_exists('_submit_check',$_POST)) {
if ($form_errors = validate_form()) {
//the form has errors or was submitted by another source
show_form($form_errors);
} else {
// The submitted data is valid, so process it
process_form();
}
} else { // The form wasn't submitted, so display
include('./incForm.php');
}
?>

Is there a good way to show the form, any errors and re-populating all
the form fields? I thought about using session variables (seeding the
form and storing values until all errors have been resolved), but am not
sure that's the way to go.

Kevin Audleman

unread,
Apr 7, 2008, 2:40:58 PM4/7/08
to

I would do two things
1. Write the form with a conditional statement next to each input (if
$error then display error message)
2. Write each element like <input type="text" id="fname" value="<?=
$_POST['fname'] />. The first time somebody comes to the form there
will be nothing in POST, so the form will be blank. Subsequent
attempts will carry forward the data they've already entered.

William Gill

unread,
Apr 7, 2008, 4:37:57 PM4/7/08
to
Kevin Audleman wrote:
> I would do two things
> 1. Write the form with a conditional statement next to each input (if
> $error then display error message)
> 2. Write each element like <input type="text" id="fname" value="<?=
> $_POST['fname'] />. The first time somebody comes to the form there
> will be nothing in POST, so the form will be blank. Subsequent
> attempts will carry forward the data they've already entered.

I had thought about putting the errors in an associative array using the
field name as the index, and then doing something like:

<?php array_key_exists('fname', $error_array) ? ?>
<input name="fname" type="text" value ="<?php echo
$post_['fname']?>"class="error">
<?php : ?>
<input name="fname" type="text" value ="<?php echo>
$post_['fname']?>">

0 new messages