如何让这个PHP表单工作?

时间:2017-08-21 16:52:56

标签: php forms

我有一个联系表格,它在另一个网站上使用相同的PHP代码并且效果很好,但是在这个上它不会因为某些原因而工作,点击发送后,它会将我重定向到一个空白页面,说明"没有提供参数!"。下面是表单的html和php。

<!-- Contact form -->
    <section id="contact_form">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <h2> We would love to hear about your upcoming project.</h2>
                    <h2 class="second_heading">Get In Touch With Us!</h2>
                </div>
                <form role="form" class="form-inline text-right col-md-6" method="post" action="mail/contact_us.php" name="sentMessage" id="contactForm" novalidate>
                    <div class="form-group">
                        <input type="text" class="form-control" id="name" placeholder="Name">
                    </div>
                    <div class="form-group">
                        <input type="email" class="form-control" id="email" placeholder="Email">
                    </div>
                    <div class="form-group">
                        <textarea class="form-control" rows="5" id="msg" placeholder="Message"></textarea>
                    </div>
                    <button type="submit" class="btn submit_btn">Submit</button>
                </form>
            </div>
        </div>
    </section><!-- Contact form end -->

<?php
// check if fields passed are empty
if(empty($_POST['name'])        ||
empty($_POST['email'])      ||
empty($_POST['msg'])    ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}

$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['msg'];

// create email body and send it
$to = 'myemail@address.com'; // send to: email
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact 
form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: 
$email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@myemail.com\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>

1 个答案:

答案 0 :(得分:3)

您的所有表单输入/ textareas都缺少name属性。例如:

<input type="email" class="form-control" id="email" placeholder="Email">

需要

<input type="email" class="form-control" id="email" name="email" placeholder="Email">

name属性是提交表单时使用的属性,因此,如果您拥有它,PHP就不会看到表单字段。