nvee Posted October 29, 2009 Share Posted October 29, 2009 Hey Guys I am in dire straits atm because I have to finish a project before Sunday and well, so far I am struggling with what appears to be the simplest tasks. In short, there is a registration form for a user. It request that the user must enter a one of three possible work telephone numbers, there must atleat be one. Additional validation requires that the one telephone number must be checked that it is indeed a numeric value. Here is my idea, and although it appears to "almost" work, it does not get the job right 100%: // validation before this part ... if ($htel == "" && $wtel == "" && $ctel == "") { $error .= "<li>We require atleast 1 telephone number</li>"; } if(isset($htel)) { $hteltest = "$htel"; if(!is_numeric($hteltest)) { $error .= "<li>Your telephone number does not appear to be numeric, please correct this</li>"; } } elseif(isset($wtel)) { $wteltest = "$wtel"; if(!is_numeric($wteltest)) { $error .= "<li>Your telephone number does not appear to be numeric, please correct this</li>"; } } elseif(isset($ctel)) { $cteltest = "$ctel"; if(!is_numeric($cteltest)) { $error .= "<li>Your telephone does not appear to be numeric, please correct this</li>"; } } else { if ($email == "") { $error .= "<li>We require your email address</li>"; } // some additional validation on the rest of the form ... What my idea is here is to check if the actual value is set with the isset, and then it should set a value to the new variable e.g. $hteltest - That variable should then be checked if it is a numberical value or not, and if it is, go on with the rest of the script, or if negative return the error. At the moment it would appear that the PHP code skips the if part of my if(isset($htel) { and still checks if the values are numerical. I hope this makes sense Quote Link to comment https://forums.phpfreaks.com/topic/179525-solved-if-on-a-if/ Share on other sites More sharing options...
jonsjava Posted October 29, 2009 Share Posted October 29, 2009 how about doing this: if(isset($htel) && !empty($htel)) { $hteltest = $htel; if(!is_numeric($hteltest)) { $error .= "<li>Your telephone number does not appear to be numeric, please correct this</li>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/179525-solved-if-on-a-if/#findComment-947295 Share on other sites More sharing options...
mikesta707 Posted October 29, 2009 Share Posted October 29, 2009 The problem is your else ifs. It will only do one of the three elseif statements (even thought they could all be true) you should change them to if's so that even if $htel isset, it will check all the others too. BTW when you assign a variable the value of another variable, you can just do $var1 = $var2; even if they are strongs. you definitely want to do this with numbers. btw instead of checking if something is a null or empty string like you do if ($string = "") just use empty if (empty($string1) || empty($string2)){ Quote Link to comment https://forums.phpfreaks.com/topic/179525-solved-if-on-a-if/#findComment-947299 Share on other sites More sharing options...
knsito Posted October 29, 2009 Share Posted October 29, 2009 Hi Just to clarify If you do this: $tel = ""; //empty string isset($tel); //variable has been set, this will return true,even if it is just an empty string Quote Link to comment https://forums.phpfreaks.com/topic/179525-solved-if-on-a-if/#findComment-947300 Share on other sites More sharing options...
nvee Posted October 29, 2009 Author Share Posted October 29, 2009 This is why I absolutely out of my heart love this website! You get answers from professionals ... quick! JonsJava: I tried your option, i copied it 3 times and merely changed the variables in each snippet. Weirdly it enters the items in the db without any values in the form. <code> } if ($dob == "") { $error .= "<li>We require your date of birth</li>"; } if(isset($wtel) && !empty($wtel)) { $wteltest = $wtel; if(!is_numeric($wteltest)) { $error .= "<li>Your telephone number does not appear to be numeric, please correct this</li>"; } } if(isset($htel) && !empty($htel)) { $hteltest = $htel; if(!is_numeric($hteltest)) { $error .= "<li>Your telephone number does not appear to be numeric, please correct this</li>"; } } if(isset($ctel) && !empty($ctel)) { $cteltest = $ctel; if(!is_numeric($cteltest)) { $error .= "<li>Your telephone number does not appear to be numeric, please correct this</li>"; } } if ($email == "") { $error .= "<li>We require your email address</li>"; } </code> Mikesta707: Thank you for the valuable tips. You can probably notice im still very new at this I tried just normal if's earlier but it still kept on going through the code although there was no value inserted in the number ... (e.g. cellular was never completed by it still returned the validation that cellular needs to be a numerical value, although the code was never completed. But I have noted the other suggestions. Knsito: You provided the solution I was declaring the variables at the top of the page with $var = $_POST['value'] irrelevant if they were empty or not, so logically the isset would be true everytime because the actual variable has been set. I merely 1st needed to check if all 3 fields was empty, and then do individual checks if the values are indeed numerical. For the record ... here is the code, I am sure it can be neater and would probably be a shorter way, but atleast it works <code> } if ($dob == "") { $error .= "<li>We require your date of birth</li>"; } if(empty($htel) && empty($wtel) && empty($ctel)) { $error .= "<li>You need to atleast have 1 telephone number</li>"; } if(!empty($wtel)) { $wteltest = $wtel; if(!is_numeric($wteltest)) { $error .= "<li>Your work telephone number does not appear to be numeric, please correct this</li>"; } } if(!empty($htel)) { $hteltest = $htel; if(!is_numeric($hteltest)) { $error .= "<li>Your home telephone number does not appear to be numeric, please correct this</li>"; } } if(!empty($ctel)) { $cteltest = $ctel; if(!is_numeric($cteltest)) { $error .= "<li>Your cellular telephone number does not appear to be numeric, please correct this</li>"; } } if ($email == "") { $error .= "<li>We require your email address</li>"; } </code> Quote Link to comment https://forums.phpfreaks.com/topic/179525-solved-if-on-a-if/#findComment-947320 Share on other sites More sharing options...
nvee Posted October 29, 2009 Author Share Posted October 29, 2009 I removed the test variables as they serve no purpose at the moment: if(empty($htel) && empty($wtel) && empty($ctel)) { $error .= "<li>You need to atleast have 1 telephone number</li>"; } if(!empty($wtel)) { if(!is_numeric($wtel)) { $error .= "<li>Your work telephone number does not appear to be numeric, please correct this</li>"; } } if(!empty($htel)) { if(!is_numeric($htel)) { $error .= "<li>Your home telephone number does not appear to be numeric, please correct this</li>"; } } if(!empty($ctel)) { if(!is_numeric($ctel)) { $error .= "<li>Your cellular telephone number does not appear to be numeric, please correct this</li>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/179525-solved-if-on-a-if/#findComment-947322 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.