使用html和php联系表单

时间:2014-08-16 17:50:54

标签: php html contact contact-form

我正在尝试使用html和php创建联系表单,但是当我提交表单而不是运行php文件时,它会在我的屏幕上显示php文件。我没有收到任何错误消息。这是我的代码:

HTML

<div class="container">
<h1>Contact Me</h1>
<div class="well">
    <p class="lead">
        Do you have any question? Want to write for us? Please use the below contact form and send a message. I'll reply you as quick as possible.
    </p>
</div>

<div class="contact-form">
<form method="post" action="form.php" class="form-horizontal col-md-8" role="form">

    <div class="form-group">
        <label for="name" class="col-md-2">Name</label>
        <div class="col-md-10">
            <input name="name" type="text" class="form-control" id="name" placeholder="Name">
        </div>
    </div>

    <div class="form-group">
        <label for="email" class="col-md-2">Email</label>
        <div class="col-md-10">
            <input name="email" type="email" class="form-control" id="email" placeholder="Email">
        </div>
    </div>

    <div class="form-group">
        <label for="subject" class="col-md-2">Subject</label>
        <div class="col-md-10">
            <input name="subject" type="subject" class="form-control" id="subject" placeholder="Subject">
        </div>
    </div>

    <div class="form-group">
        <label for="message" class="col-md-2">Message</label>
        <div class="col-md-10">
            <textarea name = "message" class="form-control" id="message" placeholder="Message"></textarea>
        </div>
    </div>

    <label>*What is 2+2? (Anti-spam)</label>
    <input name="human" placeholder="Type Here">

    <div class="form-group">
        <div class="col-md-12 text-right">
            <button type="submit" class="btn btn-lg btn-primary">Submit your message!</button>
        </div>
    </div>
</form>

PHP

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];;
$to = 'me@mariachavez.co';
$subject = $_POST['subject'];

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

if ($_POST['submit'] && $human == '4') {
    if (mail ($to, $subject, $body, $from)) {
          echo '<p>Your message has been sent!</p>';
     } else {
    echo '<p>Something went wrong, you can send an email to me@mariachavez.co and I'll get in touch soon</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>

1 个答案:

答案 0 :(得分:1)

如果没有服务器,则无法执行PHP文件,如果没有服务器,则需要安装一个,例如WAMPXAMPP

如果您有,请尝试重新启动它,或者,如果还没有,请配置HTTP服务器以执行PHP文件。