PHP / HTML联系表单语法错误不应该是错误

时间:2016-05-18 15:00:06

标签: php html forms syntax

我不知道为什么这段代码不起作用。它应该是正确的,但每次我测试时,PHP文件都会在第20行和第34行提供一个"意外的}

PHP:

<?php

if (isset($_POST["submit"])) {

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

    $from = 'website.com'; 
    $to = 'email@email.com'; 
    $subject = 'Subject';

    $body = "From: $name\n E-Mail: $email\n Messagge\n $message";

    if (mail ($to, $subject, $body, $from)) {
    $result='<div class="alert alert-success">Thanks! We\'ll reply as soon as possible.</div>';
    } else {
    $result='<div class="alert alert-danger">Sorry but there was error, try again later</div>';
    }
}  //this is line 20 on the original file
?>

HTML:

<form role="form" method="POST" action="contact.php">
    <div class="form-group">
       <input type="text" class="form-control" id="user_name" placeholder="Your name" name="name">
    </div>
    <div class="form-group">
       <input type="email" class="form-control" id="your_email" placeholder="Your email" name="email" required>
    </div>
    <div class="form-group">
       <textarea name="InputMessage" id="user_message" class="form-control" rows="5" required></textarea>
    </div>

    <button type="submit" class="btn medium" name="submit">Let us know</button>
</form> 

如果我将其删除,那么我会收到一个意外的enf文件。我不知道该怎么做,我是PHP的新手。

编辑:我检查了被标记为重复的主题,但它没有解决任何问题。对于&#34;意外}&#34;它只是说我可能忘了打开它,但我加倍检查花括号,看起来没问题。

1 个答案:

答案 0 :(得分:2)

你必须修改你的字符串,因为它里面有一个单引号

'<div class="alert alert-success">Thanks! We\'ll reply as soon as possible.</div>'