Question

I am getting the following error when I run my php script:

Parse error: syntax error, unexpected '!' in C:\xampp\htdocs\sandbox\get-started.php on line 43.

This is line 43:

!isset($_POST['budget']) || empty($_POST['budget'])

Code:

if(
    !isset($_POST['name']) || empty($_POST['name']) ||
    !isset($_POST['email']) || empty($_POST['email']) ||
    !isset($_POST['company']) || empty($_POST['company'])
    !isset($_POST['budget']) || empty($_POST['budget'])
    !isset($_POST['message']) || empty($_POST['message'])
){
}else{
    $body = "From: " . $_POST['name'] . "\n E-Mail: " . $_POST['email'] . "\n Company: " . $_POST['company'] . "\n Monthly Budget: " . $_POST['budget'] . "\n Message: " . $_POST['message'] "";
    if (mail ($to, $subject, $body, $from)) { 
        echo '<p class="success">Awesome, your message was sent!</p>';
    } else { 
        echo '<p class="error">Uh oh, something went wrong, try again.</p>'; 
    }
}

How do I fix this error?

Was it helpful?

Solution 3

You might want to reconsider the way you're doing this (see edit) but in the mean time, I suspect the following is the problem:

!isset($_POST['company']) || empty($_POST['company'])
!isset($_POST['budget']) || empty($_POST['budget'])

Add || after.

!isset($_POST['company']) || empty($_POST['company']) ||
!isset($_POST['budget']) || empty($_POST['budget']) ||

Your other error is coming from:

 $body = "From: " . $_POST['name'] . "\n E-Mail: " . $_POST['email'] . "\n Company: " . $_POST['company'] . "\n Monthly Budget: " . $_POST['budget'] . "\n Message: " . $_POST['message'] "" //<-- These are no good.

Those quotes at the end are not good, get rid of them.

$body = "From: " . $_POST['name'] . "\n E-Mail: " . $_POST['email'] . "\n Company: " . $_POST['company'] . "\n Monthly Budget: " . $_POST['budget'] . "\n Message: " . $_POST['message'];

Edit: In many cases (note I said many, not most, not all), isset() and empty() do not need to be used in conjunction. isset() checks if the value is set to anything, including 0 and ''. empty() will check if it is set and if it is, will see if it is what most people would consider an "empty" value, such as null, '', 0, and others.

So, I tend to just use !empty() or isset() which are sufficient on their own.

OTHER TIPS

You're missing || at the end of line 42, and 43.

It should be:

if(
    !isset($_POST['name']) || empty($_POST['name']) ||
    !isset($_POST['email']) || empty($_POST['email']) ||
    !isset($_POST['company']) || empty($_POST['company']) ||  // <-- here
    !isset($_POST['budget']) || empty($_POST['budget']) ||    // <-- here
    !isset($_POST['message']) || empty($_POST['message'])
)

Protip: When the compiler/parser points out a line number, don't assume that the problem is guaranteed to be on that line. It may be on the line before (like in this case), or it may be dozens of lines before. Or it may be on a line after. The key is this: the compiler/parser didn't like or expect what it saw at the given line. Why not?


At the end of this line:

$body = "From: " . $_POST['name'] . "\n E-Mail: " . $_POST['email'] . "\n Company: " . $_POST['company'] . "\n Monthly Budget: " . $_POST['budget'] . "\n Message: " . $_POST['message'] "";

you have

$_POST['message'] "";

Remove those "", or add a concatenation operator (.) to concatenate a (pointless) empty string.

You're missing some logical or (||) at the end of line 42, and line 43.

Resolve it to:

if( !isset($_POST['name']) || empty($_POST['name']) || !isset($_POST['email']) || empty($_POST['email']) || !isset($_POST['company']) || empty($_POST['company']) || //<---- !isset($_POST['budget']) || empty($_POST['budget']) || //<---- !isset($_POST['message']) || empty($_POST['message']) ){

The method you are using to check for empty and isset is very inneffecitent. Looking at the PHP documentation for the empty() function, you can see the following.

Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE

if(
   empty($_POST['name'])    ||
   empty($_POST['email'])   ||
   empty($_POST['company']) ||
   empty($_POST['budget'])  ||
   empty($_POST['message'])) 
{
   // Something is empty.
}

And if you do ever need to use isset() in the future, you can comma seperate all of the variables in one function call to check all of the variables. It will then return false if any one of the following parameters are not set, this doesn't work with empty() though.

if(isset($_POST['name'],$_POST['email'],$_POST['company'])) }
   // name, email and company are all set
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top