Syntax Error Unexpected '}'

In this code I’m getting an error of unexpected } on the line with } else {

Can you please help me correct this error? Any help is appreciated. Thanks

if (mail($to, $subject, $message, $from));
  // Set a 200 (OK) response code.
  http_response_code(200);
  echo "Thank You. Your message has been sent.";
	} else {
  // Set a 500 (internal server error) response code.
  http_response_code(500);
  echo "An error has occurred and your message could not be sent.";
}
?>

There should be no semi-colon after the if statement.

Check out https://secure.php.net/manual/en/control-structures.if.php

Swap ; for {
You have a closing bracket without a proceeding opening one.

It looks like you saw an example of alternative control structure syntax and misinterpreted the colon as a semi-colon.
http://php.net/manual/en/control-structures.alternative-syntax.php

<?php
if ($a == 5):
    echo "a equals 5";
    echo "...";
elseif ($a == 6):
    echo "a equals 6";
    echo "!!!";
else:
    echo "a is neither 5 nor 6";
endif;
?>

I prefer curly brace syntax myself, mainly because that’s what I’m used to I guess.
In any case IMHO it’s best to pick one and be consistent.

Note:
Mixing syntaxes in the same control block is not supported.

1 Like

Thanks for the replies. I have swapped the ; for {
and now have this:

“Fatal error: Call to undefined function http_response_code() on line 39”

Line 39 is:

`http_response_code(500);

Any help with remedying that error will be appreciated

That seems clear enough, n’est pas?

Can you give me an example of how to define it?

Just remove those calls to the undefined function. I’m not sure what they were intended to do, but they are superfluous.

How out-of-date is your version of PHP?

http://php.net/manual/en/function.http-response-code.php

(PHP 5 >= 5.4.0, PHP 7)
http_response_code — Get or Set the HTTP response code

Thanks for your replies.
I removed the undefined function.

Here is the code now:

<?php
$data = json_decode(file_get_contents("php://input"));
$name = trim($data->name);
$name = str_replace(array("\r", "\n"), array(" ", " "), $name);
$email = filter_var(trim($data->email), FILTER_SANITIZE_EMAIL);
$message = trim($data->message);
// Check that data was sent.
if (empty($name) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  // Set a 400 (bad request) response code and exit.
  http_response_code(400);
  echo "One or more invalid entries. Please try again.";
  exit;
}

$to = "support@...com";
$from = "From: contact-form@...com". "\r\n";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

if (mail($to, $from, $name, $email, $message)){
  echo "Thank You. Your message has been sent.";
	} else {
  echo "An error has occurred and your message could not be sent.";
}
?>

The Form successfully sends, and shows the “…your message has been sent” message on the on page, but the only Contact Form info that arrives is:

to:support@…com
and
from: From: contact-form@…com

There is no name, email nor message.

Any ideas on remedying that will be welcomed. Much thanks again.

Try putting the prameters in the order they’re expected to be in. i.e. this looks incorrect

mail($to, $from, $name, $email, $message)

http://php.net/manual/en/function.mail.php

mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
1 Like

Thanks for your reply.

I changed it to:

if (mail($to, $message, $from, $name, $email)){

but get the same result.

There is no name, email nor message delivered.

Any other help is appreciated.

But that is still not the right order. Read the manual: https://secure.php.net/manual/en/function.mail.php

When developing try using the following:

<?php 

// DEBUG && DEVELOPING
declare(strict_types=1); // PHP 7 ONLY
error_reporting(-1);
ini_set('display_errors', 'true');

/*****
 * Test function
 * usage: fred( '$value to be displayed' );
*****/
function fred( $val='No parameter???' )
{
  echo '<pre>';
    print_r( $val );
  echo '</pre>';
}//

1 Like

But not all of them:

if (empty($name) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  // Set a 400 (bad request) response code and exit.
  http_response_code(400);

@ChrisjChrisj what version of php are you running?

If you run this as a separate script:

<?php

echo phpversion();

?>

It’ll tell you what version of PHP the server is running

1 Like

5.3.29

That version of PHP isn’t supported any longer, the only 5.x version of PHP that is supported is 5.6

http://php.net/supported-versions.php

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.