PDA

View Full Version : Form Validation


cdavenport4
04-30-2007, 02:26 AM
I need to be able to validate each field. I can do this on a massive scale but I need to know where someone makes a mistake.

example of a couple of validations I'm doing now... but it puts a single entry on each time I do this.


if ($dealership) {
$query = sprintf("INSERT INTO dealershipinfo (dealership) VALUES ('$dealership')");
mysql_query($query) or die(mysql_error());
echo "<table width=\"100%\" border=\"0\"><tr><td bgcolor=\"#0099FF\"><font color=\"#FFFFFF\">Dealership Information has been added.</font></td></tr></table>";
} elseif ($submitted) {
echo "<table width=\"100%\" border=\"0\"><tr><td bgcolor=\"#CC0000\"><font color=\"#FFFFFF\">Dealership Field is empty!</font></td></tr></table>";
}

if ($address) {
$query = sprintf("INSERT INTO dealershipinfo (address) VALUES ('$address')");
mysql_query($query) or die(mysql_error());
echo "<table width=\"100%\" border=\"0\"><tr><td bgcolor=\"#0099FF\"><font color=\"#FFFFFF\">Address Information has been added.</font></td></tr></table>";
} elseif ($submitted) {
echo "<table width=\"100%\" border=\"0\"><tr><td bgcolor=\"#CC0000\"><font color=\"#FFFFFF\">Address Field is empty!</font></td></tr></table>";
}


Mysql Database
------------------------
1: Dealership
2: ------------ Address
------------------------

Any help would be great.

davidj
04-30-2007, 05:53 AM
post your whole script

personally i would validate empty fields using Javascript first then double check using PHP

cdavenport4
04-30-2007, 02:02 PM
I believe I figured it out.... I just need to make sure that there is something in form before submitting to the database.


if ($dealership == null && $submitted) {
echo "<table width=\"100%\" border=\"0\"><tr><td bgcolor=\"#CC0000\"><font color=\"#FFFFFF\">Dealership Field is empty!</font></td></tr></table>";
}

if ($address == null && $submitted) {
echo "<table width=\"100%\" border=\"0\"><tr><td bgcolor=\"#CC0000\"><font color=\"#FFFFFF\">address Field is empty!</font></td></tr></table>";
} elseif ($submitted) {

$query = sprintf("INSERT INTO dealershipinfo (dealership, address) VALUES ('$fax', 'address')");
mysql_query($query) or die(mysql_error());
echo "<table width=\"100%\" border=\"0\"><tr><td bgcolor=\"#0099FF\"><font color=\"#FFFFFF\">Thank You! Your Information has been sent.</font></td></tr></table>";
}


What do you think of my solution?

davidj
04-30-2007, 02:47 PM
bang on the money

instead of the following...


if ($address == null && $submitted) {
echo "<table width=\"100%\" border=\"0\"><tr><td


you could do this


if (! $address && $submitted) {
$message = "<table width=\"100%\" border=\"0\"><tr><td


by setting your message in a variable you can then echo the variable anywhere you want. This way gives you better control

cdavenport4
05-07-2007, 07:48 PM
I forgot to thank you for the help. So, here I go... Thank You. I've learn allot from your website.

davidj
05-07-2007, 08:42 PM
no probs

your welcome