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.

4

6 回答 6

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

于 2012-06-19T10:41:56.623 回答
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 =

于 2012-06-19T10:46:55.450 回答
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;
}
于 2012-06-19T10:48:58.473 回答
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{
于 2012-06-19T10:49:16.693 回答
0
if(empty($_POST['name']) || ......)

then failed the session.

于 2012-06-19T10:42:02.520 回答
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.

于 2012-06-19T10:52:42.010 回答