Jump to content

Recommended Posts

This is blowing my mind. I have this function and all I want it to do is check to make sure that the var $race is set and = '0'. The default value from the form is '0'. I also want it to check the value of race and if it is anything but the two listed , submit error. Yes I've tried to replace the || with &&.. Nothing works(Meaning that it will error when its not suppose to or it wont error when its suppose to).. Any ideas?

 

 

 

function check_Race ($race,$display_name,$member_id,$description,$ip) {

//echo "This is the race". $race;
//exit;

 if (isset($race) && $race ='0' && $race !== 'White' && $race !== 'Black'){

 	echo "This is an errror.!";
 	$t_error="19";
	$member_id = $member_id;
	notify_Admin($t_error,$member_id,$ip);				
   // logOut ($t_error);
	exit;

}



Link to comment
https://forums.phpfreaks.com/topic/236096-isset-with-and/
Share on other sites

function check_Race ($race,$display_name,$member_id,$description,$ip) {

//echo "This is the race". $race;
//exit;

if(isset($race)) {
	// $race isset

	switch ($race) {
		// Check if race == 0
		case 0:
			// race==0; return true
			return true;

		// Check if race == (black|white)
		case 'White':
		case 'Black':
			// Case is equal to 'Black' or 'White'
			echo "This is an errror.!";
			$t_error="19";
			$member_id = $member_id;
			notify_Admin($t_error,$member_id,$ip);				
		   // logOut ($t_error);
			exit;
	}
}
}	

 

I've commented the code so you understand what each section is doing. If your unsure about switch statements click here

Link to comment
https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213773
Share on other sites

This is my html from the form

 

<select size="1" name="race">
	<option value="0"></option>
			<option value="White">White / Caucasian </option>
			<option value="Black">Black / African American </option>
			<option value="Hispanic">Hispanic </option>
			<option value="Asian">Asian / Pacific Islander </option>

			<option value="Arabic">Arabic / Middle Eastern</option>
			<option value="Native_american">Native American Indian</option>
			<option value="Other">Other</option>

		  </select></div>

Link to comment
https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213782
Share on other sites

This grabs all the post vars

 

$_POST = array_map('strip_tags', $_POST);
    $_POST = preg_replace( "^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$^", " ", $_POST );
    $_POST = array_map('mysql_real_escape_string', $_POST);
   // array_walk_recursive($_POST, 'filter');
    extract($_POST,EXTR_SKIP);

 

This is the function call

check_Race ($race,$display_name,$member_id,$description,$ip); // Check Race

 

I also echo'd out the $race var before the call and inside the function it's self and the var has the expected value.

Link to comment
https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213803
Share on other sites

your if doesn't contain an else so whenever the value of $race has changed to another value that is not white, black or 0 the function is supposed not to do anything.

This is the same with the switch.

Also I don't see how the if can be executed when the value of $race is white or black.

Have you posted the entire function or just a part of it?

 

Hey micmania1,

 

That didn't work.. When I chose a different option that "White" or "Black" it did not display the error..

 

Any ideas?

basically your if looks like this: if $race is set and race = 0 and race is not black and race is not white

then how can the if be executed when $race = hispanic?

 

also I don't get why you are doing this:

 

$member_id = $member_id;

 

what is the point in that? You have already passed $member_id inside the function. Declaring $member_id as $member_id does not make sense to me :S?

Link to comment
https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213807
Share on other sites

your if doesn't contain an else so whenever the value of $race has changed to another value that is not white, black or 0 the function is supposed not to do anything.

This is the same with the switch.

Also I don't see how the if can be executed when the value of $race is white or black. please ignore this line in my last post...

Have you posted the entire function or just a part of it?

 

Hey micmania1,

 

That didn't work.. When I chose a different option that "White" or "Black" it did not display the error..

 

Any ideas?

basically your if looks like this: if $race is set and race = 0 and race is not black and race is not white

then how can the if be executed when $race = hispanic?

 

also I don't get why you are doing this:

 

$member_id = $member_id;

 

what is the point in that? You have already passed $member_id inside the function. Declaring $member_id as $member_id does not make sense to me :S?

 

I failed to modify my post, so sorry for this double post...

 

if (isset($race) && $race !== 'White' && $race !== 'Black'){

}

 

don't you want to do something like this instead?

The if will now be executed when $race = anything but black or white

Link to comment
https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213811
Share on other sites

I tried that already.. About every combo you can think of.. i finally just changes it from a drop down to a radio button and it works fine..I don't understand the difference..

 


function check_Race ($race,$display_name,$member_id,$description,$ip) {

 if (isset($race) && $race != 'White' && $race != 'Black'){	
     	     	
	 $t_error="19";
	$member_id = $member_id;
	notify_Admin($t_error,$member_id,$ip);				
	logOut ($t_error);
	exit;

}


}

 

Radio button code

 

<div class="hcenter"><div class="box3">Ethnicity</div></div>
	<div class="box2"><input type="radio" name="race" value="White" />White<br />
        <input type="radio" name="race" value="Black" />Black<br /></div>

 

 

Link to comment
https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213819
Share on other sites

Like spiderwell said, the way you're attempting to use isset is rather pointless.  You don't want to check if $race exists within your check_race() function, but rather before you attempt to pass it into the function.  In other words, like this:

 

if (isset($race))
{
   check_race($race /* other args */ );
}
else
{
   // system error, as $race does not exist
}

 

For your original, drop down scenario, did you ever actually see if $race existed, or contained the proper test value?  Basic debugging 101: if a value isn't acting as expected, echo it out to the screen to see if it's what you think it should be.

Link to comment
https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213823
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.