2010-06-06 40 views
1

我正在處理PHP聯繫表單,但我無法使其工作。我得到以下錯誤在Apache服務器日誌,在Ubuntu的服務器虛擬機上運行:

PHP Parse error: syntax error, unexpected $end in /home/matthew/Sites/contactFormResponse.php on line 75, referer: http://192.168.1.4/contactForm.php 

從谷歌上搜索這個錯誤,這聽起來像它的正常使用,當服務器沒有設置到短PHP代碼或者引起識別它們,或者讓一段未正確關閉的代碼。但據我所知,情況並非如此 - 就我所見,它全部正確關閉。它所指的行是文件末尾的一行。

這裏是PHP代碼:

<?php 
            error_reporting(E_ALL); 
            // Define variables to hold the name, email address and message, and import the information into the variables 
            $name = $_POST['NameInput']; 
            $email = $_POST['EmailAddress']; 
            $telno = $_POST['ContactNumber']; 
            $querytype = $_POST['QueryType']; 
            $bookingstartdate = $_POST['BookingStartDay'] . $_POST['BookingStartMonth'] . $_POST['BookingStartYear']; 
            $bookingenddate = $_POST['BookingEndDay'] . $_POST['BookingEndMonth'] . $_POST['BookingEndYear']; 
            $message = $_POST['QueryText']; 

            // Validate the inputs - send it if it's OK 
            if(3 < strlen($name) && 3 < strlen($email)) 
            { 
              $email_message = <<< EMAIL 
                Message from contact form at holidaychalet.co.uk 
                Name: $name 
                Email: $email 
                Contact Number: $telno 
                Query Type: $querytype 
                Booking Start Date: $bookingstartdate 
                Booking End Date: $bookingenddate 
                The message: 
                $message 
                EMAIL; 
              $headers = "cc:[email protected]\r\n"; 
              if(mail('[email protected]','Contact form email', $email_message, $headers)) 
              { 
                echo "Thanks for completing the form! I'll be in touch shortly!"; 
              } 
              else 
              { 
                echo "Something went wrong - please use the back button and try again"; 
              } 
            } 
            else 
            { 
              echo "You didn't complete the form fully enough! Please use go back using your web browser's back button"; 
            } 
          ?> 
+0

這些問題對SO政策。 – 2010-06-06 18:07:41

+0

@Col。彈片爲什麼?我在常見問題解答中看不到任何關於它的信息,而且我在其他地方找不到答案。 – 2010-06-06 18:19:10

+0

你的問題並不違反政策,我不知道Col Shrapnel也在談論什麼。 – Amber 2010-06-06 18:28:55

回答

5

here document syntax結束標識符必須在一行的開始,沒有任何壓痕:

這是非常重要的一點必須指出,行結束標識符必須不包含其他字符,除了可能分號(;)。這意味着特別是標識符可能不縮進,並且在分號之前或之後可能沒有任何空格或製表符。認識到關閉標識符之前的第一個字符必須是由本地操作系統定義的換行符也很重要。

所以你的情況:

          $email_message = <<< EMAIL 
                Message from contact form at holidaychalet.co.uk 
                Name: $name 
                Email: $email 
                Contact Number: $telno 
                Query Type: $querytype 
                Booking Start Date: $bookingstartdate 
                Booking End Date: $bookingenddate 
                The message: 
                $message 
EMAIL; 
+1

+1不幸的是,這是它的方式,在大多數情況下HEREDOC無效。 – 2010-06-06 18:05:52

+1

呃?它仍然是完全可用的...具有不縮進的關閉標識符不會終止代碼的可讀性。實際上,考慮到heredoc *內容的縮進空格將包含在生成的字符串中,因此它可以讓開發人員意識到他們將字符串放入的內容。 – Amber 2010-06-06 18:06:36

+0

乾杯,這就解決了問題! – 2010-06-06 18:15:53

4
EMAIL; 

不能縮進。 Heredoc語法要求結束標識符位於行的開始,並且不包含前導空白。

3

您正在填充$ email_message,其中包含一個標記爲以EMAIL結尾的字符串;

這一個必須在一行。

將其更改爲:

          $email_message = <<< EMAIL 
                Message from contact form at holidaychalet.co.uk 
                Name: $name 
                Email: $email 
                Contact Number: $telno 
                Query Type: $querytype 
                Booking Start Date: $bookingstartdate 
                Booking End Date: $bookingenddate 
                The message: 
                $message 
EMAIL;