0

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.

sark9012
  • 5,485
  • 18
  • 61
  • 99

6 Answers6

2
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

Bono
  • 4,757
  • 6
  • 48
  • 77
  • @ManseUK Haha lol, I double added it now, but no worries ;) I didn't want to "steal" anyones answer. Though shouldnt matter I guess. Thanks again :) – Bono Jun 19 '12 at 10:55
  • Perfect answer, broken everything down in explanation. The == was just a silly error on my behalf, I know the rules surrounding that area well. Oops! Cheers dude. – sark9012 Jun 19 '12 at 11:21
  • @Luke No problem, glad I could be of help ;) – Bono Jun 19 '12 at 11:29
1
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 =

Gntem
  • 6,949
  • 2
  • 35
  • 48
1

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;
}
dhh
  • 4,289
  • 8
  • 42
  • 59
1

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{
FatalError
  • 922
  • 12
  • 31
0
if(empty($_POST['name']) || ......)

then failed the session.

Sudip Pal
  • 2,041
  • 1
  • 13
  • 16
0

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.

PEM
  • 1,948
  • 14
  • 13
  • Like this http://stackoverflow.com/questions/4007752/php-check-if-variable-exist-but-also-if-has-a-value-equal-to-something – Manse Jun 19 '12 at 10:54