2014-01-08 57 views
1

我嘗試了很多方法來解決這個問題,但好像我碰到了牆壁。它應該與這種方式的工作,但我無法找到任何錯誤,因爲這是寫有一個直接用電子郵件聯繫表格,使用php

語法錯誤,意想不到的$結束

後我點擊提交按鈕。

首先,我輸入這樣的代碼。

<form class="form" method="post" action="sendContact.php"> 
       <table> 
        <tr> 
         <td class="contact-firstcol"> <label for="name">Name</label> </td> 
         <td class="contact-secondcol"> : </td> 
         <td class="contact-thirdcol"> <input type="text" name="name" id="name" /> </td> 
        </tr> 
        <tr> 
         <td class="contact-firstcol"> <label for="email">Email</label> </td> 
         <td class="contact-secondcol"> : </td> 
         <td class="contact-thirdcol"> <input type="text" name="email" id="email" /> </td> 
        </tr> 
        <tr> 
         <td class="contact-firstcol"> <label for="phone">Phone</label> </td> 
         <td class="contact-secondcol"> : </td> 
         <td class="contact-thirdcol"> <input type="text" name="phone" id="phone" /> </td> 
        </tr> 
        <tr> 
         <td class="contact-firstcol"> <label for="message">Message</label> </td> 
         <td class="contact-secondcol"> : </td> 
         <td class="contact-thirdcol"> <textarea id="message" name="message"></textarea> </td> 
        </tr> 
        <tr> 
         <td class="contact-firstcol"></td> 
         <td class="contact-secondcol"></td> 
         <td class="contact-thirdcol"> <input type="submit" name="submit" value="SUBMIT" /> </td> 
       </table>  
      </form> 

該文件名爲sendContact.php

  <?php 
      $to = '[email protected]'; 
      $subject = 'from email contact'; 

      $name = $_POST ['name']; 
      $email = $_POST ['email']; 
      $phone = $_POST ['phone']; 
      $message = $_POST ['message']; 

      $body = <<< EMAIL 
      Hi! My name is $name 
      $message. 
      From : $name 
      Email : $email 
      Topic : $topic 
      EMAIL; 
      $header = "From: $email"; 

      if (isset($_POST)) { 
       if ($name == '' || $email == '' || $topic == '' || $message = '') { 
        $feedback = 'Please fill in any fields.'; 

       } else { 

       mail($to, $subject, $body, $header); 
       $feedback = 'Thanks for the information, we will get back to you in 24 hours.'; 
       } 
      } 
?> 
<p class="feedback"> <?php echo $feedback ?> </p> 

我已經作出這樣簡單,也確保對電子郵件的細節將在輕鬆閱讀。你能指出上面的錯誤嗎?

乾杯

回答

18

不能有<<<EMAIL之間的任何空格<<< EMAIL

它必須是<<<EMAIL(見注瞭解更多信息)

諮詢:

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

heredoc

實施例#1無效示例在參數的更多信息(從手動)

<?php 
class foo { 
    public $bar = <<<EOT 
bar 
    EOT; 
} 
?> 

實施例#2定界符串引述例如

<?php 
$str = <<<EOD 
Example of string 
spanning multiple lines 
using heredoc syntax. 
EOD; 

/* More complex example, with variables. */ 
class foo 
{ 
    var $foo; 
    var $bar; 

    function foo() 
    { 
     $this->foo = 'Foo'; 
     $this->bar = array('Bar1', 'Bar2', 'Bar3'); 
    } 
} 

$foo = new foo(); 
$name = 'MyName'; 

echo <<<EOT 
My name is "$name". I am printing some $foo->foo. 
Now, I am printing some {$foo->bar[1]}. 
This should print a capital 'A': \x41 
EOT; 
?> 

實施例#3定界符示例

<?php 
var_dump(array(<<<EOD 
foobar! 
EOD 
)); 
?> 

和你的情況:(我測試工作)

我改變$topic$phone否則它會拋出一個錯誤。

您將需要進一步修改您的PHP以反映適當的更改。

<?php 
$to = '[email protected]'; 
$subject = 'from email contact'; 

$name = $_POST ['name']; 
$email = $_POST ['email']; 
$phone = $_POST ['phone']; 
$message = $_POST ['message']; 

$body = <<<EMAIL 
Hi! My name is $name 
$message. 
From : $name 
Email : $email 
Topic : $topic 
EMAIL; 
$header = "From: $email"; 

if (isset($_POST)) { 
    if ($name == '' || $email == '' || $phone == '' || $message = '') { 
     $feedback = 'Please fill in any fields.'; 

    } else { 

    mail($to, $subject, $body, $header); 
    $feedback = 'Thanks for the information, we will get back to you in 24 hours.'; 
    } 
} 
?> 
<p class="feedback"> <?php echo $feedback ?> </p> 

腳註:

而作爲Kostis在評論說--- 「這是一個不能有在前面任何空白結束標記同樣重要的是要認識到。關閉標識符之前的第一個字符必須是由本地操作系統定義的換行符。「


+0

它仍然沒有工作過。我很困惑什麼是錯。有時候它會垃圾郵件。我不明白爲什麼。有什麼,我錯過? – Anthosiastic

+0

實際這是不正確的,它是前面不能有任何空格的結束標籤「認識到結束標識符之前的第一個字符必須是本地操作系統定義的換行符也很重要(來自您提供的鏈接) – Kostis

+0

那裏也有好點@Kostis –