我尝试了很多方法来解决这个问题,但似乎我碰到了墙。它应该以这种方式工作,但我找不到任何错误,因为它写的是
单击“提交”按钮后语法错误,意外的$ end
。
首先,我输入这样的代码。
<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 = 'abc@abc.com';
$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>
我已经做到了这么简单,并确保能够轻松阅读电子邮件中的详细信息。你能指出上面的任何错误吗?
干杯
答案 0 :(得分: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 Heredoc字符串引用示例
<?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参数示例中的Heredoc
<?php
var_dump(array(<<<EOD
foobar!
EOD
));
?>
,在您的情况下:(我测试过并且正在工作)
我将$topic
更改为$phone
,否则会引发错误。
您需要进一步修改PHP以反映相应的更改。
<?php
$to = 'abc@abc.com';
$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在评论中所说的那样--- “这是结束标记,前面不能有任何空白。同样重要的是要认识到结束标识符之前的第一个字符必须是由本地操作系统。“