dsp77 Posted June 25, 2010 Share Posted June 25, 2010 i have a form that i validate by: if(isset($hasErrorName)) if(isset($hasErrorLastName)) it works but when i want to verify it doesent work if(!isset($hasErrorName,$hasErrorLastName)) { some code } Quote Link to comment https://forums.phpfreaks.com/topic/205837-isset-dilemma/ Share on other sites More sharing options...
KevinM1 Posted June 25, 2010 Share Posted June 25, 2010 i have a form that i validate by: if(isset($hasErrorName)) if(isset($hasErrorLastName)) it works but when i want to verify it doesent work if(!isset($hasErrorName,$hasErrorLastName)) { some code } Mind showing your actual code? Quote Link to comment https://forums.phpfreaks.com/topic/205837-isset-dilemma/#findComment-1077098 Share on other sites More sharing options...
dsp77 Posted June 25, 2010 Author Share Posted June 25, 2010 //If the form is submitted if(isset($_POST['submit'])) { //Check to make sure that the nume field is not empty if(trim($_POST['nume']) == '') { $hasErrorNume = true; } else { $nume = trim($_POST['nume']); } //Check to make sure that the prenume field is not empty if(trim($_POST['prenume']) == '') { $hasErrorPrenume = true; } else { $prenume = trim($_POST['prenume']); } //Check to make sure that the organizatie field is not empty if(trim($_POST['organizatie']) == '') { $hasErrorOrganizatie = true; } else { $organizatie = trim($_POST['organizatie']); } //Check to make sure sure that a valid email address is submitted if(trim($_POST['email_1']) == '') { $hasErrorEmail_1 = true; } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email_1']))) { $hasErrorEmail_1_e = true; } else { $email = trim($_POST['email_1']); } //If there is no error, send the email if(!isset($hasErrorNume)) { $emailTo = 'youremail@yoursite.com'; //Put your own email address here $body = "Name: $name \n\nEmail: $email \n\nSubject: $nume \n\nComments:\n $prenume"; $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email_1; mail($emailTo, $subject, $body, $headers); $emailSent = true; } } Quote Link to comment https://forums.phpfreaks.com/topic/205837-isset-dilemma/#findComment-1077099 Share on other sites More sharing options...
KevinM1 Posted June 25, 2010 Share Posted June 25, 2010 Nothing jumps out at me as being wrong. That said, what I like to do in these cases is use one variable to represent both the error case and the filtered value if validation checks out. In other words, something like: if (!empty(trim($_POST['name']))) { $name = trim($_POST['name'])); } else { $name = false; } // ... if ($name) // $name is true AND contains a legit value { // email code } This saves one from checking to see if an error variable exists or not. You know you'll always have a value for that input. You only need to see if it's legit or false/error. Quote Link to comment https://forums.phpfreaks.com/topic/205837-isset-dilemma/#findComment-1077106 Share on other sites More sharing options...
bluejay002 Posted June 25, 2010 Share Posted June 25, 2010 Going to your example: (!isset($hasErrorNume)) It seems that $hasErrorNume is being assigned with a boolean value. I think you don't need to use isset() here but directly apply ! instead like: (!$hasErrorNume) I think that would suffice but if you are still wary about it... you can just add it up with &&, you don't have to go so much with complication, IMHO. bluejay, Quote Link to comment https://forums.phpfreaks.com/topic/205837-isset-dilemma/#findComment-1077109 Share on other sites More sharing options...
dsp77 Posted June 25, 2010 Author Share Posted June 25, 2010 when i submit the form it only checks for $hasErrorNume if its valid and goes on but if the rest $hasErrorPrenume $hasErrorOrganizatie etc are empty it still goes on if i add if(!isset($hasErrorNume,$hasErrorPrenume,$hasErrorOrganizatie)) it still check only for the firstone and i want to check for all errors Quote Link to comment https://forums.phpfreaks.com/topic/205837-isset-dilemma/#findComment-1077111 Share on other sites More sharing options...
KevinM1 Posted June 25, 2010 Share Posted June 25, 2010 Going to your example: (!isset($hasErrorNume)) It seems that $hasErrorNume is being assigned with a boolean value. I think you don't need to use isset() here but directly apply ! instead like: (!$hasErrorNume) I think that would suffice but if you are still wary about it... you can just add it up with &&, you don't have to go so much with complication, IMHO. bluejay, He has to use isset because the error variables won't exist if the form values validate. In other words: if (trim($_POST['nume']) == '') { $hasErrorNume = true; // $hasErrorNume exists (is set) and contains true } else { $nume = trim($_POST['nume']); // $hasErrorNume does NOT exist (is not set) } --- if(!isset($hasErrorNume' date='$hasErrorPrenume,$hasErrorOrganizatie)) it still check only for the firstone and i want to check for all errors[/quote'] What precisely is happening? What data are you passing in, and what is the result? Is it still sending out an e-mail even if the other fields are empty? Quote Link to comment https://forums.phpfreaks.com/topic/205837-isset-dilemma/#findComment-1077123 Share on other sites More sharing options...
bluejay002 Posted June 25, 2010 Share Posted June 25, 2010 He has to use isset because the error variables won't exist if the form values validate. Not necessarily. You see if you use: (!$hasErrorNume) and this variable is not initialized, it will still go in because it will return false. Since what he would do here is run the following statements that the said variable is not true then the same idea would still be valid. Otherwise the he wants to do some strong typing development, then he needs to do else for his if so that it will always exist. Quote Link to comment https://forums.phpfreaks.com/topic/205837-isset-dilemma/#findComment-1077155 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.