sark9012
sark9012

Reputation: 5727

Checking that all form fields aren't empty

I have added a checker to ensure that all fields in a form have data within when the form is submitted.

if(     (   isset($_POST['name'] == "Contact")  )
    && (    isset($_POST['company'] == "Company Name")  )
    && (    isset($_POST['address'] == "Address")   )
    && (    isset($_POST['turnover'] == "Approx. Turnover") )
    && (    isset($_POST['employees'] == "No. Of Employees")    ) 
    && (    isset($_POST['contact'] == "Contact Number")    )  
  )
    {
    //nothing has changed and we fail the form
    $_SESSION['failed'] == true;
}
else{

I am getting the following error:

Parse error: syntax error, unexpected T_IS_EQUAL, expecting ',' or ')'

Can anyone see what the problem is? Also, will this check that all post values have data within?

Thanks.

Upvotes: 0

Views: 473

Answers (6)

FatalError
FatalError

Reputation: 964

I think, logically you should check for empty and the default values with OR operator

if(     empty($_POST['name']) ||  $_POST['name'] == "Contact"  
||     empty($_POST['company']) ||  $_POST['company']  == "Company Name"
||     empty($_POST['address']) ||  $_POST['address']  == "Address"
||     empty($_POST['turnover']) ||  $_POST['turnover']  == "Approx. Turnover"
||    empty($_POST['employees']) ||  $_POST['employees']  == "No. Of Employees"
||    empty($_POST['contact']) ||  $_POST['contact']  == "Contact Number"


 )

 {
    //nothing has changed and we fail the form
    $_SESSION['failed'] = true;
}
else{

Upvotes: 1

Bono
Bono

Reputation: 4849

if(isset($_POST['name']) == "Contact") 

should be

if(isset($_POST['name']) && $_POST['name'] == "Contact") 

(or you can use || if you want OR)

Also, it does not check if it has a value. It only checks if a variable is set. Use either == '' or empty() to check if it's got a value.

And

$_SESSION['failed'] == true;

should be

$_SESSION['failed'] = true;

== is for comparison and = is for assignment

Upvotes: 2

PEM
PEM

Reputation: 1978

You could also setup an array of what you expect, and make a diff between POST array and expected array, if there is any diff, then fail... That is, if you have hard coded check values... but it seems you want to check the value and not only the key, so it should fit your needs.

Upvotes: 0

dhh
dhh

Reputation: 4337

The function isset (http://www.php.net/manual/function.isset.php) is used for checking whether your variable is defined. To check if a value is entered you should use something like:

if(
    empty($_POST['name']) || $_POST['name'] == "Contact"
    || empty($_POST['company']) || $_POST['company'] == "Company Name"
)
{
    $_SESSION['failed'] = true;
}

Upvotes: 1

Gntem
Gntem

Reputation: 7155

if(   
   (isset($_POST["name"]) && $_POST["name"] == "Contact")&& 
   (    isset($_POST['company']) && $_POST["Company Name"] == "Company Name") && 
   (    isset($_POST['address']) && $_POST["Address"] == "Address")   && 
   (    isset($_POST['turnover']) && $_POST["Approx. Turnover"] == "Approx. Turnover") && 
   (    isset($_POST['employees'])&&  $_POST["No. Of Employees"] == "No. Of Employees")    && 
    (    isset($_POST['contact']) && $_POST["Contact Number"] == "Contact Number")  
    ){
    $_SESSION["failed"]=TRUE;
    }

you forgot to close the isset() and at the line with $_SESSION you must assign with =

Upvotes: 1

Sudip
Sudip

Reputation: 2051

if(empty($_POST['name']) || ......)

then failed the session.

Upvotes: 0

Related Questions