使用html和php联系表单

时间:2022-10-17 11:34:35

I am trying to create a contact form using html and php, but when I submit the form instead of running the php file it displays the php file on my screen. I do not get any error message. This is my code:

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

HTML

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

<?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 个解决方案

#1


1  

You can't execute PHP files without server, if you don't have it, you need to install one, for example WAMP or XAMPP.

没有服务器就无法执行PHP文件,如果没有服务器,则需要安装一个,例如WAMP或XAMPP。

If you have one, try restarting it, or , if you haven't, configure HTTP server to execute PHP files.

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

#1


1  

You can't execute PHP files without server, if you don't have it, you need to install one, for example WAMP or XAMPP.

没有服务器就无法执行PHP文件,如果没有服务器,则需要安装一个,例如WAMP或XAMPP。

If you have one, try restarting it, or , if you haven't, configure HTTP server to execute PHP files.

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