son Posted May 4, 2011 Share Posted May 4, 2011 Hi there,I run this simple add data form which takes inputs and puts all in database. The data inserts ok, but I have trouble with the error message that should appear if a user has not selected an option (first option 0 is "Please select"). The code for check routine is: if (!isset($_POST['imgCat']) OR ($_POST['imgCat']) < 0) { $errors[$imgCat] = 'Please select a Page'; $imgCat = FALSE; } else { $imgCat = $_POST['imgCat']; } At top of this page I have also $imgCat = FALSE; along with the other variables from form. Why is the error not displaying ok? I check with right name in form as <?php echo check_error('imgCat', 'Page*'); ?>Son Link to comment Share on other sites More sharing options...
dsonesuk Posted May 4, 2011 Share Posted May 4, 2011 the 'please select' option has value of 0 but you are looking for value less than 0, also $_POST VALUES will be string so it will be comparing "0" to 0, use (int) to convert to integer value. Link to comment Share on other sites More sharing options...
son Posted May 4, 2011 Author Share Posted May 4, 2011 the 'please select' option has value of 0 but you are looking for value less than 0, also $_POST VALUES will be string so it will be comparing "0" to 0, use (int) to convert to integer value.Where would I need to do the integer conversion? Beforeif (!isset($_POST['imgCat']) OR ($_POST['imgCat']) < 0){ ?Thanks,Son Link to comment Share on other sites More sharing options...
birbal Posted May 4, 2011 Share Posted May 4, 2011 at the time of comparison data type in php will automaticaly type casted (execpt '===' or '!=='). If your 'please select' value is 0 then as dsonusk said you need to check that the $_POST['imgcat'] value is zero$_POST['imgCat']==0 Link to comment Share on other sites More sharing options...
dsonesuk Posted May 4, 2011 Share Posted May 4, 2011 you don't use OR in if condition, it should be similar to below if (!isset($_POST['imgCat']) || ((int)$_POST['imgCat']) === 0) Link to comment Share on other sites More sharing options...
son Posted May 4, 2011 Author Share Posted May 4, 2011 you don't use OR in if condition, it should be similar to below if (!isset($_POST['imgCat']) OR ((int)$_POST['imgCat']) == 0) { $errors[$imgCat] = 'Please select a Page'; $imgCat = 0; } else { $imgCat = $_POST['imgCat']; } It simply does not do it. I also tried to have it exactly as you had it with "||" and "===", but it does also not work. Can you choose if you use || or OR and == or ===? I have never used it this way...Son Link to comment Share on other sites More sharing options...
thescientist Posted May 4, 2011 Share Posted May 4, 2011 I think this is what birbal was suggesting with typecastinghttp://us3.php.net/manual/en/function.intval.phpcomparisons operators (==, ===) are different than logical operators (||/OR). You just need to know what they do. Link to comment Share on other sites More sharing options...
dsonesuk Posted May 4, 2011 Share Posted May 4, 2011 you have to find out if it is text string or numeric, if numeric check if value 0, similar to below$valid = true;$errors="";if (!isset($_POST['imgCat']) || $_POST['imgCat'] =="" || is_numeric($_POST['imgCat']) && ((int)$_POST['imgCat']) === 0){$errors.= 'Please select a Page <br />';$valid = false;}else{$imgCat =$_POST['imgCat'];}if($valid){echo "saving to database";}else{echo $errors;echo 'Invalid Value show form again';} Link to comment Share on other sites More sharing options...
son Posted May 4, 2011 Author Share Posted May 4, 2011 I feel really stupid, I simply cannot get it working. I have tried several things now (all the suggested stuff) and with: if (!isset($_POST['imgCat']) || $_POST['imgCat'] =="" || is_numeric($_POST['imgCat']) && ((int)$_POST['imgCat']) === 0) { $errors[$imgCat] = 'Please select a Page'; $imgCat = 0; } else { $imgCat = $_POST['imgCat']; } ($imgCat = 0; or $imgCat = FALSE; at top) simly not doing it...My error check funtion is in place as: function check_error ($field, $text) { global $errors; if (empty($errors[$field])) { echo $text; } else { echo "<strong>" . $errors[$field] . "*</strong>"; } } and my check in form label should also be ok as:<label for="imgCat"><?php echo check_error('imgCat', 'Page*'); ?></label> What is going on?Son Link to comment Share on other sites More sharing options...
birbal Posted May 5, 2011 Share Posted May 5, 2011 I think this is what birbal was suggesting with typecastinghttp://us3.php.net/manual/en/function.intval.php actually i meant that its dont need to type cast explicitly. when comparison/arithmetic oparator used it auto typecasted. eg$a='sometext';if($a==0)echo 'matched';elseecho 'did not mathced'; at first it may look like it will show did not match but it will echo matched. cause when the comparison oprator being appliedthe string in $a get auto type casted. and string become 0 (if the left most character is not a integer) when it type casted to integer.it happens same with other data types maintaining the table of data type conversion listed in php.net, untill '===' or '!==' is being used. in the above code if '===' is used it will show did not matched cause strict compraiosn will not let php to auto type cast. it will try to match the value as well as data type of the value.so from the $_POST['var']='0' if now $_POST['var'] is check against integer 0 the $_POST['var'] will be converted to ineteger and the be compared. Link to comment Share on other sites More sharing options...
son Posted May 5, 2011 Author Share Posted May 5, 2011 actually i meant that its dont need to type cast explicitly. when comparison/arithmetic oparator used it auto typecasted. egif (($_POST['imgCat']) === 0) { $imgCat = 0; $errors[$imgCat] = 'Please select a Slideshow Page'; } else { $imgCat = $_POST['imgCat']; } Using at bottom of page var_dump($imgCat); it shows the right $imgCat each time I change the value. The error message still does not show. Really do not get it...Son Link to comment Share on other sites More sharing options...
dsonesuk Posted May 5, 2011 Share Posted May 5, 2011 look what you checking for a error with? 'imgCat' a string?<label for="imgCat"><?php echo check_error('imgCat', 'Page*'); ?></label> should be<label for="imgCat"><?php echo check_error($imgCat, 'Page*'); ?></label> Link to comment Share on other sites More sharing options...
dsonesuk Posted May 5, 2011 Share Posted May 5, 2011 try it withif(!isset($_POST['imgCat'])){$imgCat = 1;}else if( isset($_POST['imgCat']) && is_numeric($_POST['imgCat']) && ((int)$_POST['imgCat']) === 0){$imgCat = 0;$errors[$imgCat] = 'Please select a Page';}else{$imgCat = $_POST['imgCat'];} Link to comment Share on other sites More sharing options...
son Posted May 9, 2011 Author Share Posted May 9, 2011 I tried your suggetions, still not working (and var_dump always show correct cat nr). No sign of error message. The function is function check_error ($field, $text) { global $errors; if (empty($errors[$field])) { echo $text; } else { echo "<strong>" . $errors[$field] . "*</strong>"; } } and should not need a $ sign (string should be fine). I use this also as <?php echo check_error('img1', 'Foto (Max 2MB)*'); ?> for an image and the error message shows fine when I forget to upload...really do not understand what is going wrong here....Son Link to comment Share on other sites More sharing options...
dsonesuk Posted May 9, 2011 Share Posted May 9, 2011 It therefore should be match the reference value passed to errors[] $errors[$imgCat] = 'Please select a Slideshow Page'; should be$errors['imgCat'] = 'Please select a Slideshow Page';which now matches <label for="imgCat"><?php echo check_error('imgCat', 'Page*'); ?></label>which when called, will look to see if $errors['imgCat'] has warning text assigned to it, if false/empty show normal text else show error message. For this to work it should be similar to this <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php function check_error ($field, $text) { global $errors; if (empty($errors[$field])) { // if matching $field = 'imgCat' (as in error['imgCat']) does not have 'Please select a Page' assigned to it (as in empty) echo $text; // show 'Page*' } else { // else show errors[$field] (as in error['imgCat']) with 'Please select a Page' assigned to it echo "<strong>" . $errors[$field] . "*</strong>"; } } if( isset($_POST['imgCat']) ) { if(is_numeric($_POST['imgCat']) && ((int)$_POST['imgCat']) === 0) {$errors['imgCat'] = 'Please select a Page'; } else { $imgCat = $_POST['imgCat']; } } ?> <form method="post" action="thispage.php" enctype="multipart/form-data"> <label for="imgCat"><?php echo check_error('imgCat', 'Page*'); ?></label> <select name="imgCat"> <option value="0"> Please select</option> <option value="1">first_option</option> <option value="2">second_option</option> <option value="3">third_option</option> </select> <input type="submit" /> </form> </body> </html> As you can see there is no need for $imgCat =false; or $imgCat =true; unless it was used for some reason for the input for file upload. Link to comment Share on other sites More sharing options...
son Posted May 10, 2011 Author Share Posted May 10, 2011 Cheers dsonesuk! I cannot believe that I did not see this myself. Obviously I sneaked in there an $ which does not belong there... I have now if(isset($_POST['imgCat']) && is_numeric($_POST['imgCat']) && ((int)$_POST['imgCat']) != 0){$imgCat = $_POST['imgCat'];}else{$imgCat = 0;$errors['imgCat'] = 'Please select a Page';} working, which is great.Many thanks,Son Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.