Search code examples
phphtmlformsparse-error

Why am I getting this parse error?


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?


Solution

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