Jump to content

[SOLVED] NULL || 0 or "" || 0 Validation not working??? HELP PLEASE!!!???


StefanRSA

Recommended Posts

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

Link to comment
Share on other sites

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
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
}
?>

Link to comment
Share on other sites

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!  :-[

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.