...

View Full Version : Keep values in HTML form with PHP



WRXHokie
03-26-2007, 03:32 PM
I'm trying to process a form. In my data validation function i try and check for errors in the user submitted fields. In the fields in which are fine i want to be able to keep the values in the form when there are errors and clear the fields that have errors in them. For some reason i cannot get this to work. Here's my code, i've cut it down to the relevant parts. The first file is the index.php file which shows the form but first checks for errors. The second file has all the relevant php functions i call defined. In form.php i just show a small piece of the show form function where i try and present a text field with its default value.



<?php
require "form.php";
$defaults = array();

if ($_POST['_submit_check']) {
// If validate_form() returns errors, pass them to show_form()
if ($form_errors = validate_form($defaults)) {
show_form($form_errors, $defaults);
} else {
process_form();
}
} else {
show_form();
}

?>


form.php:

<?php

function show_form($errors = '', $defaults) {

print '<input type="text" name="person" size="1" maxlength="2" value=';
print $defaults[person];
print '>';

}


function process_form() {
//TODO: MySQL statements to add to database
show_form();
}

function validate_form($defaults = '') {
// Start with an empty array of error messages
$errors = array();
$role_array = array();

if (!is_numeric($_POST['person'])) {
$defaults[person] = "";
$errors[] = 'Please enter a valid person id (numeric)';
}
else {
$defaults[person] = $_POST['person'];
}


if(!isset($_POST['production'])) {
$errors[] = 'Please select a production';
}


if(isset($_POST['role'])) {
$role_array = $_POST['role'];
}
else
$errors[] = 'Please select at least one role';

// Return the (possibly empty) array of error messages
return $errors;
}

?>

I'm lost, so what am i missing?

Fumigator
03-26-2007, 03:49 PM
In the future please wrap your code with [ PHP ] and [ /PHP ] tags (remove the spaces). Much easier to read for us.

Re: your problem at hand, it looks like you have a scope issue. When you use a variable inside a function, it is accessable to only that function unless you declare the variable as "global" inside the function. So where you are assigning a value to $defaults['person'] inside your validate_form() function, that value's not making it out of the function. Add the line "global $defaults;" at the top of the function to solve this immediate problem, but going down the road you will want to design your code in a way that minimizes (or completely eliminates) global variables, as they make code less usable for a number of reasons.

WRXHokie
03-26-2007, 04:04 PM
Ah... that make sense.

If i define it in just that file (form.php) outside the scope of any function and then add values within the functions, will it keep its values from function to function?

Fumigator
03-26-2007, 04:14 PM
If i define it in just that file (form.php) outside the scope of any function and then add values within the functions, will it keep its values from function to function?

Not without the "global" statement. The variable inside the function, although named the same, does not point to the same address(es) in memory as the variable outside the function.

WRXHokie
03-26-2007, 04:21 PM
Thanks for the help man... but it didnt work for some reason.

Still not keeping the value even with it defined as global inside form.php

Here's the code as it stands now:



form.php:

<?php

global $defaults

function show_form($errors = '') {

print '<input type="text" name="person" size="1" maxlength="2" value="';
print "$defaults[person]";
print '">';

}

function process_form() {
//TODO: MySQL statements to add to database
show_form();
}

function validate_form() {
// Start with an empty array of error messages
$errors = array();
$role_array = array();

if (!is_numeric($_POST['person'])) {
$defaults[person] = "";
$errors[] = 'Please enter a valid person id (numeric)';
}
else {
$defaults[person] = $_POST['person'];
}


if(!isset($_POST['production'])) {
$errors[] = 'Please select a production';
}


if(isset($_POST['role'])) {
$role_array = $_POST['role'];
}
else
$errors[] = 'Please select at least one role';

// Return the (possibly empty) array of error messages
return $errors;
}
?>


index.php:

<?php
require "form.php";

if ($_POST['_submit_check']) {
// If validate_form() returns errors, pass them to show_form()
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
process_form();
}
} else {
show_form();
}

?>

aedrin
03-26-2007, 04:23 PM
want to be able to keep the values in the form when there are errors and clear the fields that have errors in them.

This would be very frustrating as a user.

How would I know what I did wrong (even when told what the problem was), if I don't see the input again?

WRXHokie
03-26-2007, 04:27 PM
This would be very frustrating as a user.

How would I know what I did wrong (even when told what the problem was), if I don't see the input again?

No i want to clear the fields that have errors and keep the fields that dont have errors.

Fumigator
03-26-2007, 05:50 PM
Put the global statement inside every function you need access to the global variable in. And don't forget the semi-colon ;)



function validate_form() {
global $defaults;
.
.
.
}


p.s. What Aedrin means is it would be better to alert the user when something wrong has been entered, but leave the wrong stuff in the field so the user can see what was entered... just makes it easier for the user to understand what he did wrong.

WRXHokie
03-26-2007, 06:07 PM
Put the global statement inside every function you need access to the global variable in. And don't forget the semi-colon ;)



function validate_form() {
global $defaults;
.
.
.
}


p.s. What Aedrin means is it would be better to alert the user when something wrong has been entered, but leave the wrong stuff in the field so the user can see what was entered... just makes it easier for the user to understand what he did wrong.


I understand what your saying about leaving the wrong stuff in the field, but thats not standard practice when you enter data into a form that is incorrect. Every form i've ever used deletes the data from the fields that have been incorrectly filled in and then displays how to correctly enter that data in that field. I think it would be counter intuitive to do it the opposite way.

WRXHokie
03-26-2007, 06:12 PM
Thanks man, that worked.

aedrin
03-26-2007, 06:45 PM
but thats not standard practice when you enter data into a form that is incorrect.

I've never encountered a form that removes incorrect data I entered.

Imagine, typing in a 20 digit account number. You forget the dash after the 3rd number. Oops, have to type it all in again!

Still sound counter intuitive to remove all the data that is incorrect?

The ONLY exception to this is passwords. But passwords should never be repeated in a form. Even if they are correct.


Put the global statement inside every function you need access to the global variable in. And don't forget the semi-colon

The global statement should be avoided whenever possible. It's bad for maintenance and pollutes the global variable space.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum