2010-06-06 36 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;