2013-02-25 74 views
2

为什么我的PHP的邮件代码总是给:语法错误,意想不到的 'A A A A'(T_STRING)

语法错误,意想不到的 'A A A A'(T_STRING)在C:\ XAMPP \ htdocs目录\ GSP \ members.php第4行

<?php 
if(!isset($_POST['email'])) { 
      
    $email_to = '[email protected]'; //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._%-][email protected][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

您保存文档的编码是什么? – 2013-02-25 12:53:08

+2

有时候会发生这样的情况,你会不经意地保存一些奇特的不道德字符。尝试真的删除该行并重写它。 – arkascha 2013-02-25 12:54:18

+0

和哪个编辑器? – 2013-02-25 12:54:28

回答

6

你的问题是PHP文件已经保存了异国情调的空白字符 - 也就是说不是标准空格,而是一些其他字符被渲染为空格(甚至根本不显示),但是PHP不能解析。

删除第3行和第4行的所有空白字符,重新输入并保存文件。 (即全部为空格,包括换行符和单词之间的空格)

这应该可以解决问题。

如果执行此操作后仍然出现错误,但在另一行上,则需要重复该行的过程。

+0

谢谢,我的问题解决了家伙 – 2013-02-26 11:56:25

+2

这也解决了我的问题,只是提到,我从互联网上复制粘贴一些代码时,出现了这个问题,从SyntaxHighlighter到Sublime Text 2。 – jOpacic 2013-05-22 10:59:46

0

我看到你的代码中的一些错误:

尝试更换:

$email_to = '[email protected]'; 

有了:

$email_to = "[email protected]"; 

而在这行:

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:尝试用双引号( '' 与 “”)替换所有单引号我所有字符串瓦尔)

Saludos。

+0

为什么双引号要比单引号好? – Niko 2013-02-25 13:03:55

+0

这样更好,因为通过这种方式,你的代码更具可读性,并且以这种方式你没有混合的东西.....我总是使用一种方式,或另一种方式,但从来没有混合过的东西.... 。对我来说那些混合=将来很难找到错误;) – Hackerman 2013-02-25 13:19:14

+2

@RobertRozas - 混合的引用风格是完全合法的,并且完全可读。对于这个问题的答案,它没有什么可做的。 – SDC 2013-02-25 14:39:28