Testing forms before submitting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Azhrarn
Forum Newbie
Posts: 12
Joined: Mon Dec 15, 2003 4:32 am

Testing forms before submitting

Post by Azhrarn »

Hi guys,
Mr Newbie again :P
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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Azhrarn
Forum Newbie
Posts: 12
Joined: Mon Dec 15, 2003 4:32 am

Post 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!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Azhrarn
Forum Newbie
Posts: 12
Joined: Mon Dec 15, 2003 4:32 am

Post 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
Post Reply