...

View Full Version : checking fields



bunny1
03-08-2007, 09:46 AM
Hi

I have a form that the user fills in details.
I want to make sure only numbers are in certain fields and letters in others.
I also want to make sure all fields are filled in before a user can continue.

What's the best way of alerting a user?
Are message boxes a good idea and how can these be done in php?

thanks

lansing
03-08-2007, 01:57 PM
Here is a option for using JS

http://javascript.internet.com/forms/validate-numeric-only.html

Here is the php solution
http://us3.php.net/manual/en/function.is-numeric.php

To make sure the fields aren't just blank use this:
if ($variable1 == '' || $variable2 == '' || $variable3 == '')
{
echo'Please fill all fields';
}
else
{
//process form
}

JohnDubya
03-08-2007, 04:12 PM
This is how I, personally, do it:

After the user submits his input, I declare it:


$first_name = $_POST['first'];
$how_many_kids = $_POST['kids'];

Then I run checks on them with ctype_ functions. For numbers only (no decimal points or anything else), use ctype_digit(). For letters only, use ctype_alpha().

As for a message, I just throw any errors into a $Message variable and then echo it at the top of my pages. If there is no error, nothing shows up. I also specify $Error_Stat = 0 at the top of my page and change that to 1 if there is an error...and then only run the MySQL query if the $Error_Stat still equals 0.


<?php
$Error_Stat = 0;

if (!ctype_alpha($first_name)) {
$Error_Stat = 1;
$Message = 'Your first name must be only letters!';
}

if (!ctype_digit($how_many_kids)) {
$Error_Stat = 1;
$Message = 'Amount of kids must be only numbers!';
}

if ($Error_Stat == 0) {
//do the query, insert, etc. here
}
?>

<?=$Message?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum