PHP mail error: parse error on line 12

When I run this it says "PHP Syntax Check: Parse error: syntax error, unexpected '}' in your code on line 12 -

if(mail) {$success == true};

PHP

$to = "Admin@mywebsite.com";

$subject = $_POST["Subject"];

$message = 'Hello World!';

$headers = 'From: User@Yourwebsite.com' . "rn" .
  'Reply-To: ' . $_POST["Email_From"] . "rn" .
  'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

if(mail) {$success == true};
else {$success = false};

if($success){print "<meta http-equiv="refresh" content="0;URL=thanks.html">"};
else{print "<meta http-equiv="refresh" content="0;URL=error.html">"};

You have a lot of syntax issues. Here is the cleaned up code:

$to = "Admin@mywebsite.com";

$subject = $_POST['Subject'];

$message = 'Hello World!';

$headers = 'From: User@Yourwebsite.com' . "rn" .
  'Reply-To: ' . $_POST['Email_From'] . "rn" .
  'X-Mailer: PHP/' . phpversion();

$mail_result = mail($to, $subject, $message, $headers);

if ($mail_result) {
  $success = true;
}
else {
  $success = false;
}

if($success){
  print '<meta http-equiv="refresh" content="0;URL=thanks.html">';
}
else{
  print '<meta http-equiv="refresh" content="0;URL=error.html">';
}

First, your error shows as:

"PHP Syntax Check: Parse error: syntax error, unexpected '}' in your code on line 12

Which is this line:

if(mail) {$success == true};

Two major issues. First you are checking mail which is not a defined constant or even a variable. That should be something like what I set $mail_result with the ( $ ) in front of the name. Then your assignment of $success == true is not an assignment. That == is strictly a comparison operator; it should be = for assignment. Then there is no ; at the end of the assignment, but it appears outside of the braces. Syntax wise, that is not correct. I changed that whole chunk to:

$mail_result = mail($to, $subject, $message, $headers);

if ($mail_result) {
  $success = true;
}
else {
  $success = false;
}

Now $mail_result is connected to mail() as a boolean value. Then the if() checks that. The $success is now setting proper assignments with = and the line ends with ; as it should.

Then these two lines are filled with syntax issues as well:

if($success){print "<meta http-equiv="refresh" content="0;URL=thanks.html">"};
else{print "<meta http-equiv="refresh" content="0;URL=error.html">"};

You have the same issue with ; as the previous issue. But also, your double-quotes ( " ) for the string containment conflict with the double quotes used for the <meta… tags. So using single quotes ( ' ) for the string containment here & formatting as well.

if($success){
  print '<meta http-equiv="refresh" content="0;URL=thanks.html">';
}
else{
  print '<meta http-equiv="refresh" content="0;URL=error.html">';
}

But now with the code working, it might make sense to considerate the $mail_result setting with the $success check like this:

$mail_result = mail($to, $subject, $message, $headers);

if($mail_result){
  print '<meta http-equiv="refresh" content="0;URL=thanks.html">';
}
else{
  print '<meta http-equiv="refresh" content="0;URL=error.html">';
}

And it doesn't make much sense to assign true or false when that is what mail() returns to begin with.

And that said, you might not need the <meta http-equiv="refresh"… depending on your final code goal. Superficially you can use the header function in PHP to better effect like this.

$mail_result = mail($to, $subject, $message, $headers);

if($mail_result){
  header('Location: thanks.html');
}
else{
  header('Location: error.html');
}
链接地址: http://www.djcxy.com/p/69890.html

上一篇: 查询产生语法错误

下一篇: PHP邮件错误:第12行解析错误