Page 1 of 1
Testing forms before submitting
Posted: Tue Dec 23, 2003 4:03 am
by Azhrarn
Hi guys,
Mr Newbie again

I have a set of forms and I want values to be entered into them before submitting. Is there any way, before I try to update the database, to check if the forms have something in them, and make a popup message box appear to warn if it doesn't?
Also, is it possible to test the content for the form?
Cheers and Marry Xmas to all of you!
Azhrarn
Posted: Tue Dec 23, 2003 4:10 am
by twigletmac
If you want popup boxes then you need to use Javascript. It's best to not rely simply on clientside validation (which can easily be turned off by users) so you'll also need to test the data using PHP. You can [php_man]trim[/php_man]() all the input,
Code: Select all
foreach ($_POST as $key => $value) {
$_POST[$key] = trim($value);
}
to get rid of extra spaces, then you can use functions like [php_man]empty[/php_man]() to make sure that there is data in the field. If you need to test for a number then there are functions like [php_man]is_numeric[/php_man]() and [php_man]ctype_digit[/php_man]() that you can use.
Enjoy,
Mac
Posted: Tue Dec 23, 2003 4:31 am
by Azhrarn
Thx!
Just one more question:
suppose I have two forms, how do I test this stuff and POST if they re ok, and not post if they re not?
Thanks!
Posted: Tue Dec 23, 2003 5:23 am
by twigletmac
You test the data using the relevant functions, you would then probably use an if ... else statement to say (pseudo code won't run)
Code: Select all
if (data is ok) {
// insert data into database
} else {
// show an error message and allow user to correct form
}
Mac
Posted: Tue Dec 23, 2003 5:54 am
by Azhrarn
yeah, but here is the problem:
I post in the main page and I update the database in the following page (POST from one to the other). So I want to test before posting, in such a way that someone can still edit the fields before loading the next page. Is there any way to do this, or will I at least have to reload the current page?
Cheers and thanks for all the help.
Azh