StefanRSA Posted May 5, 2009 Share Posted May 5, 2009 Hi, I am trying to do a sort of validation to get an error message if the following code is running. $utest = $attributes['units']; //echo $utest; if ($utest == 0 || $utest =='') { $errmsg .= "<FONT COLOR='Red'><p><b>Please indicate how many rooms/units is needed for this booking</p></font>"; } else { $errmsg = ""; } if ($errmsg != "") { echo $errmsg ."<b>Fill out the accommodation unit fields <div id='next'><a href=\"javascript:history.back();\">Click here</a></div>"; exit; } This is not working, (I think its because I set $utest = $attributes['units'] I have read all over and start to understand that I cannot really test for $utest to be = to "" or NULL. Does anybody have an indication on what I should be doing??? Thanks Stefan Quote Link to comment https://forums.phpfreaks.com/topic/156940-solved-null-0-or-0-validation-not-working-help-please/ Share on other sites More sharing options...
suncore Posted May 5, 2009 Share Posted May 5, 2009 Try this one ( though, I haven't changed anything - formating and explanations added ) - works for me :-\ <?php $utest = $attributes['units']; // Assign unit array to a single variable /* UNIT IS NOW EMPTY */ if ($utest == 0 || $utest =='') { // If unit is empty ( 0 or zero-byte string ) $errmsg .= "<FONT COLOR='Red'><p><b>Please indicate how many rooms/units is needed for this booking</p></font>"; // Add some text to error message } else { // If unit contains some data $errmsg = ""; // Clear error message } if ($errmsg != "") { // If error message is NOT empty echo $errmsg ."<b>Fill out the accommodation unit fields <div id='next'><a href=\"javascript:history.back();\">Click here</a></div>"; // Echo this text exit; // Exit } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156940-solved-null-0-or-0-validation-not-working-help-please/#findComment-826658 Share on other sites More sharing options...
Maq Posted May 5, 2009 Share Posted May 5, 2009 This is not working, (I think its because I set $utest = $attributes['units'] Why not? Are you supposed to assign $utest to that array? What is the value of $utest? Your code looks fine, I'm not sure what the issue is here... Please elaborate. Quote Link to comment https://forums.phpfreaks.com/topic/156940-solved-null-0-or-0-validation-not-working-help-please/#findComment-826662 Share on other sites More sharing options...
StefanRSA Posted May 5, 2009 Author Share Posted May 5, 2009 Ty Suncore.... I am new to php and have not comment on my code as yet.... But it make sense to do it! Thanks! Maq, if I echo $utest; Nothing Echo's but the $errmsg stay empy??? That is why I set - if ($utest == 0 || $utest ==').... It does not make sense??? $attributes['units]; gets populated only with numbers or nothing??? Should I mabe test for a numerical value??? How do I do that? So, If $utest = 0 or not a value the error should work. Can it be done? Quote Link to comment https://forums.phpfreaks.com/topic/156940-solved-null-0-or-0-validation-not-working-help-please/#findComment-826674 Share on other sites More sharing options...
suncore Posted May 5, 2009 Share Posted May 5, 2009 You may want to try this code ( just test it by changing unit count from 1 to 0 and so on .. ) : <?php $attributes = array('units' => 0); // Your array where unit equals to 0 ( ZERO ) $unit = $attributes['units']; // Assign array to a single variable if ($unit == 0 || empty($unit)) { // If 0 or empty echo "Error : unit field is empty or equals to zero!"; // Error } else { // If NOT empty and is greater than 0 ( 1,2,3 .. ) echo "Unit field contains some data "; // Success } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156940-solved-null-0-or-0-validation-not-working-help-please/#findComment-826683 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 suncore, if $unit is 0, empty($unit) is true too so there's no need to check if $unit is 0. Quote Link to comment https://forums.phpfreaks.com/topic/156940-solved-null-0-or-0-validation-not-working-help-please/#findComment-826688 Share on other sites More sharing options...
StefanRSA Posted May 5, 2009 Author Share Posted May 5, 2009 TY GUYS!!! Quote Link to comment https://forums.phpfreaks.com/topic/156940-solved-null-0-or-0-validation-not-working-help-please/#findComment-826800 Share on other sites More sharing options...
Maq Posted May 5, 2009 Share Posted May 5, 2009 You mind posting the final solution, so others can easily refer to it? Quote Link to comment https://forums.phpfreaks.com/topic/156940-solved-null-0-or-0-validation-not-working-help-please/#findComment-826802 Share on other sites More sharing options...
StefanRSA Posted May 5, 2009 Author Share Posted May 5, 2009 My problem was actually not the empty or 0 value!!!! I used it within the following code: if($attributes['units']!=0 || $attributes['adults']!=0 || $attributes['children']!=0 || $attributes['infants']!=0){ THIS IS WHERE I PUT THE CODE!!!!! } It now reads: foreach($_POST['fields'] AS $row=>$attributes){ $utest = $attributes['units']; if ($unit == 0 || $unit == '' || empty($unit)) { $errmsg .= "<FONT COLOR='Red'><b><p>$utest Rooms selected!!!</p><p>Please indicate how many rooms/units is needed for this booking</p></font>"; } else { $errmsg = ""; } if ($errmsg != "") { echo $errmsg ."<b>Fill out the accommodation unit fields <div id='next'><a href=\"javascript:history.back();\">Click here</a></div>"; exit; } if($attributes['units']!=0 || $attributes['adults']!=0 || $attributes['children']!=0 || $attributes['infants']!=0){ //you need to send accom_type as a hidden field in the form $full_set .= $attributes['accom_type'].','.$attributes['pp_pr'].','.$attributes['units'].','.$attributes['adults'].','.$attributes['children'].','.$attributes['infants'].';'; $full_set1 .= $attributes['accom_type'].', '.$attributes['units'].' unit/s, '.$attributes['adults'].' adult/s, '.$attributes['children'].' children, '.$attributes['infants'].' infant/s;'; }} THANKS FOR YOUR HELP!!!! I am still a php ROOKIE! Quote Link to comment https://forums.phpfreaks.com/topic/156940-solved-null-0-or-0-validation-not-working-help-please/#findComment-826820 Share on other sites More sharing options...
Maq Posted May 5, 2009 Share Posted May 5, 2009 Like Ken mentioned earlier, empty refers to all of these conditions, so you only need to check for empty. You should trim first as well. if (empty(trim($unit))) { Here's the scenarios when empty returns true: * "" (an empty string) * 0 (0 as an integer) * "0" (0 as a string) * NULL * FALSE * array() (an empty array) * var $var; (a variable declared, but without a value in a class) Quote Link to comment https://forums.phpfreaks.com/topic/156940-solved-null-0-or-0-validation-not-working-help-please/#findComment-826825 Share on other sites More sharing options...
StefanRSA Posted May 5, 2009 Author Share Posted May 5, 2009 GREAT learning curve!!! Again! THANKS GUYS!!! Quote Link to comment https://forums.phpfreaks.com/topic/156940-solved-null-0-or-0-validation-not-working-help-please/#findComment-826834 Share on other sites More sharing options...
StefanRSA Posted May 5, 2009 Author Share Posted May 5, 2009 OOPS! Maq, If I use if (empty(trim($unit))) { I get the following error...? Fatal error: Can't use function return value in write context in /home/thehost/public_html/online/add1a.php on line 425 Quote Link to comment https://forums.phpfreaks.com/topic/156940-solved-null-0-or-0-validation-not-working-help-please/#findComment-826839 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Yeah, you should do: $unit = trim($unit); if (empty($unit)) { ... } Quote Link to comment https://forums.phpfreaks.com/topic/156940-solved-null-0-or-0-validation-not-working-help-please/#findComment-826846 Share on other sites More sharing options...
Maq Posted May 5, 2009 Share Posted May 5, 2009 OOPS! Maq, If I use if (empty(trim($unit))) { I get the following error...? Fatal error: Can't use function return value in write context in /home/thehost/public_html/online/add1a.php on line 425 Yeah sorry... Ken posted the correct code. Quote Link to comment https://forums.phpfreaks.com/topic/156940-solved-null-0-or-0-validation-not-working-help-please/#findComment-826865 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.