为什么我的PHP邮件代码总是给出:
语法错误,第4行的C:\ xampp \ htdocs \ GSP \ members.php中的意外“”(T_STRING)
<?php
if(!isset($_POST['email'])) {
$email_to = 'kennydharmawan@gmail.com'; //this is line 4
$email_subject = "GSP Rent Order";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['unit']) ||
!isset($_POST['startdate']) ||
!isset($_POST['enddate']) ||
!isset($_POST['telephone']) ||
!isset($_POST['rname']) ||
!isset($_POST['city']) ||
!isset($_POST['adress'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name'];
$email_from = $_POST['email'];
$unit = $_POST['unit'];
$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];
$telephone = $_POST['telephone'];
$rname = $_POST['rname'];
$city = $_POST['city'];
$adress = $_POST['adress'];
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
$phone_exp = "/^[1-9][0-9]{0,15}$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$rname)) {
$error_message .= 'The Recipient Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$city)) {
$error_message .= 'The City you entered does not appear to be valid.<br />';
}
if(!preg_match($phone_exp,$telephone)) {
$error_message .= 'The Phone Number you entered does not appear to be valid.<br />';
}
if(strlen($adress) < 2) {
$error_message .= 'The Adress you entered do not appear to be valid.<br />';
}
list($dd,$mm,$yyyy) = explode('/',$startdate);
if (!checkdate($mm,$dd,$yyyy)) {
$error_message .= 'The Start Date you entered do not appear to be valid.<br />';
}
list($dd,$mm,$yyyy) = explode('/',$enddate);
if (!checkdate($mm,$dd,$yyyy)) {
$error_message .= 'The End Date you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Unit: ".clean_string($unit)."\n";
$email_message .= "Start Date: ".clean_string($startdate)."\n";
$email_message .= "End Date: ".clean_string($enddate)."\n";
$email_message .= "Telephone Number: ".clean_string($telephone)."\n";
$email_message .= "Recipient Name: ".clean_string($rname)."\n";
$email_message .= "Recipient City: ".clean_string($city)."\n";
$email_message .= "Recipient Adress: ".clean_string($adress)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
echo "Thank you for contacting us. We will be in touch with you very soon."
}
?>
为什么它一直都在说这个错误?
答案 0 :(得分:6)
你的问题是PHP文件是用异国空白字符保存的 - 即不是标准空格,而是一些其他字符呈现为空格(甚至根本没有显示),但是PHP无法解析。 / p>
删除第3行和第4行的所有空格字符,重新键入它们,然后保存文件。 (那是所有空白区域,包括换行符和单词之间的空格)
这应该可以解决问题。
如果在执行此操作后仍然出现错误,但在另一行上,则您还需要重复该行的过程。
答案 1 :(得分:0)
我在您的代码中看到了一些错误:
尝试更换:
$email_to = 'kennydharmawan@gmail.com';
使用:
$email_to = "kennydharmawan@gmail.com";
在这一行:
echo "Thank you for contacting us. We will be in touch with you very soon."
你错过了一个“;”,所以修好了:
echo "Thank you for contacting us. We will be in touch with you very soon.";
PS:尝试用双引号替换所有单引号(''with“”)我所有的字符串变量;)
Saludos。