corillo181 Posted March 17, 2007 Share Posted March 17, 2007 i making this function to check for empty fields but i'm having no success at all. function emptyfields($fname,$password,$password2,$city){ if(!($fname)){ return false; }else{ return true; } if(!($password)){ return false; }else{ return true; } if(!($password2)){ return false; }else{ return true; } if(!($city)){ return false; }else{ return true; } } here the out put if(isset($_POST['submit'])){ include($_SERVER['DOCUMENT_ROOT'].'/functions/formcheck.php'); $username=$_POST['username']; $email=$_POST['email']; $fname=$_POST['fname']; $password=$_POST['password']; $password2=$_POST['password2']; $city=$_POST['city']; if(emptyfields($fname,$password,$password2,$city)){ echo "<br /> all feilds are good"; }else{ echo "<br /> all fields need to be full"; } } Link to comment https://forums.phpfreaks.com/topic/43105-function-question/ Share on other sites More sharing options...
Barand Posted March 17, 2007 Share Posted March 17, 2007 Your main problem is that whatever the outcome when you check the name field, you return from the function, so other fields never get checked. To me it would be more readable if a function called emptyfields() returned "true" if there were empty fields. ie if (emptyfields(....) ) echo "Some fields are empty"; else echo "OK" Link to comment https://forums.phpfreaks.com/topic/43105-function-question/#findComment-209394 Share on other sites More sharing options...
corillo181 Posted March 17, 2007 Author Share Posted March 17, 2007 mm i didn't understand but you are right the is my main problem it function only check to see that one field is full and it says the whole statement is true . i need to make it that chekc for every field and not just one. Link to comment https://forums.phpfreaks.com/topic/43105-function-question/#findComment-209446 Share on other sites More sharing options...
corillo181 Posted March 17, 2007 Author Share Posted March 17, 2007 well i checkged my function to look this way, if any one know a better way to lay out a form to chekc for empty fields please correct my code. function emptyfields($fname,$password,$password2,$city){ if(!($fname) || !($password) || !($password2) || !($city)){ return true; }else{ return false; } } Link to comment https://forums.phpfreaks.com/topic/43105-function-question/#findComment-209454 Share on other sites More sharing options...
per1os Posted March 17, 2007 Share Posted March 17, 2007 <?php $checkFields = array($fname, $password, $password2, $city); if (emptyfields($checkFields)) Die("You have an empty field!"); function emptyfields($fields) { if (is_array($fields) { foreach($fields as $field) { if (empty($field) || trim($field) == "") return true; } }else return true; //no array to check return false; // everything went fine. } ?> Link to comment https://forums.phpfreaks.com/topic/43105-function-question/#findComment-209477 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.