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
sh33p1985
Forum Commoner
Posts: 78 Joined: Thu Mar 11, 2004 9:22 am
Post
by sh33p1985 » Mon Oct 30, 2006 11:57 am
ive created a simple function to check whether the fields in a submitted form are empty and also that some of them are of the correct format:
Code: Select all
function checkFields($productName, $productDesc, $unitPrice, $stockLevel){
//Check for empty fields
if(empty($productName) || empty($productDesc) || empty($unitPrice) || empty($stockLevel)){
header ("location: ../add_product.php?error=1");
exit;
}
//Check formats
if(!is_double($unitPrice)){
header ("loation: ../add_product.php?error=2");
exit;
}
if(!is_integer($stockLevel)){
header ("loation: ../add_product.php?error=3");
exit;
}
}
ive tested this by entering some test data, all valid except for inputting a string instead of an integer in the StockLevel textfield but instead of getting the format error, i get the empty field error and im having a hard time correcting it.
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Mon Oct 30, 2006 12:16 pm
I'm a bit confused.. try a
var_dump() on all your variables to see what they contain and post back here please.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Mon Oct 30, 2006 12:20 pm
Are you passing data for the rest of the fields? If any of them are empty it gets redirected to error 1...
Code: Select all
<?php
if ( empty($productName) || empty($productDesc) || empty($unitPrice) || empty($stockLevel) )
{
header ("location: ../add_product.php?error=1");
exit;
}
?>
sh33p1985
Forum Commoner
Posts: 78 Joined: Thu Mar 11, 2004 9:22 am
Post
by sh33p1985 » Mon Oct 30, 2006 12:20 pm
string(1) "1" string(1) "1" float(9.99) int(0)
heres the problem and im 95% of the solution too. bare with me 2 mins
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Mon Oct 30, 2006 12:37 pm
0 will return false when passed to !empty()
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Mon Oct 30, 2006 12:55 pm
Are you type casting or
intval() 'ing before you send the information to the function?
sh33p1985
Forum Commoner
Posts: 78 Joined: Thu Mar 11, 2004 9:22 am
Post
by sh33p1985 » Mon Oct 30, 2006 1:04 pm
i was typecasting before the empty field check, which would result in $unitPrice and $stockLevel evaluating to false, ive seperated the methods now and im currently working on reimplementing it so it works properly.