Jump to content

function question


corillo181

Recommended Posts

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

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

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

<?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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.