dk4210 Posted May 11, 2011 Share Posted May 11, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/236096-isset-with-and/ Share on other sites More sharing options...
micmania1 Posted May 11, 2011 Share Posted May 11, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213773 Share on other sites More sharing options...
spiderwell Posted May 11, 2011 Share Posted May 11, 2011 isset it pointless as you have built the function to have it as a required argument. Quote Link to comment https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213778 Share on other sites More sharing options...
dk4210 Posted May 11, 2011 Author Share Posted May 11, 2011 Hey micmania1, That didn't work.. When I chose a different option that "White" or "Black" it did not display the error.. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213781 Share on other sites More sharing options...
dk4210 Posted May 11, 2011 Author Share Posted May 11, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213782 Share on other sites More sharing options...
micmania1 Posted May 11, 2011 Share Posted May 11, 2011 Can you show me an example of how you are using the function and also what result you are getting. Quote Link to comment https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213796 Share on other sites More sharing options...
dk4210 Posted May 11, 2011 Author Share Posted May 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213803 Share on other sites More sharing options...
PHPSuperNewb Posted May 11, 2011 Share Posted May 11, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213807 Share on other sites More sharing options...
PHPSuperNewb Posted May 11, 2011 Share Posted May 11, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213811 Share on other sites More sharing options...
dk4210 Posted May 11, 2011 Author Share Posted May 11, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213819 Share on other sites More sharing options...
KevinM1 Posted May 11, 2011 Share Posted May 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213823 Share on other sites More sharing options...
dk4210 Posted May 11, 2011 Author Share Posted May 11, 2011 Yea I a little above basic... I did echo out the vars to see if what I was expected is what it echo'd out and it was.. Thanks for the help.. It works with radio buttons but not a drop down..I'm Baffled.. I will just use radio button instead.. Quote Link to comment https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213824 Share on other sites More sharing options...
KevinM1 Posted May 11, 2011 Share Posted May 11, 2011 Did you try using a different value for your default/non-selected option? PHP may have been treating your 0 as an actual integer, not a string. Quote Link to comment https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213827 Share on other sites More sharing options...
dk4210 Posted May 11, 2011 Author Share Posted May 11, 2011 Actually I didn't try that... Quote Link to comment https://forums.phpfreaks.com/topic/236096-isset-with-and/#findComment-1213828 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.