PHP联系表单不会确认已发送的消息

时间:2015-01-23 21:21:14

标签: php contact-form

在重新设计一页网站后,我注意到联系表单无法正常工作。它接受输入,但页面不会重新加载。即使在单击“提交”之前,PHP确认消息也是可见的。我也没有收到收集数据的电子邮件。我从旧网站版本的上一个工作联系表单中复制了这个PHP,并试图让它适合新代码。我已经匹配了所有变量并检查了拼写错误。到目前为止,我没有看到代码有任何差异,但它显然不起作用。 ?

在错误日志中,代码显示:

[24-Jan-2015 15:07:54 UTC] PHP Parse error:  syntax error, unexpected '{', expecting '(' in /home/website/public_html/index.php on line 664

听664是下面的行开头“

<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$from = 'From: Contact Form'; 
$to = 'me@email.com'; 
$subject = 'Inquiry';

$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";

if ($name != '' && $email != '')
 {
    if (mail ($to, $subject, $body, $from))
    {
        echo '<p>Your message has been sent.</p>';
    } 
    else 
    { 
        echo '<p>Something went wrong, go back and try again.</p>'; 
    }
 }  
else 
{
      echo '<p>You need to fill in all required fields.</p>';
}
}
?>

以下是所有联系表格:

<div class="col-md-6 contact-form">
<h1>Get In touch</h1>
<div class="contact" data-scroll-reveal="enter left">
<form id="contact-form" action="index.php" method="post">
<ul>
<li>
<input type="text" name="name" id="name" placeholder="Name">
</li>
<li>
<input type="text" name="number" id="number" placeholder="Number">
</li>
<li>
<input type="email" name="email" id="email" placeholder="E-Mail">
</li>
<li>
<textarea name="message" id="message" placeholder="Message">   </textarea>
<input class="btnsend" name="submit" type="submit" id="submit" value="Send">
</li>
</ul>
</form>
</div>
</div>

1 个答案:

答案 0 :(得分:1)

将所有代码放入if中,如@jeroen所说,使用isset,您也可以将最后一个代码放入if ($name,例如:

if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $number = $_POST['number'];
    $message = $_POST['message'];
    $from = 'From: Blue Mountain Website Contact Form'; 
    $to = 'cassidy@blumount.com'; 
    $subject = 'Inquiry';

    $body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";

    if ($name != '' && $email != '') {
        if (mail ($to, $subject, $body, $from)) {
            echo '<p>Your message has been sent.</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again.</p>'; 
        }
    }  else {
        echo '<p>You need to fill in all required fields.</p>';
    }
}