简体   繁体   English

联系表格不起作用

[英]Contact form doesnt work

i wrote code for contact form and i dont know why it doesnt work.我为联系表编写了代码,但我不知道为什么它不起作用。 Everything is set up with form but i didnt receive email.一切都用表格设置,但我没有收到电子邮件。 I didnt get any error or something.我没有得到任何错误或什么。 This is the html code :这是html 代码

<!-- Form -->
<form method="post" action="contact_form.php" name="contactform" id="contactform" class="row">
    <fieldset>
        <div class="form-field col-sm-6">
            <label for="name">Name</label>
            <span><input type="text" name="name" id="name" /></span>
        </div>
        <div class="form-field col-sm-6">
            <label for="email">Email</label>
            <span><input type="email" name="email" id="email" /></span>
        </div>
        <div class="form-field col-sm-6">
            <label>*What is 2+2? (Anti-spam)</label>
            <span><input name="human" placeholder="Type Here"></span>
        </div>
        <div class="form-field col-sm-12">
            <label for="message">Message</label>
            <span><textarea name="message" id="message"></textarea></span>
        </div>
    </fieldset>
    <div class="form-click center col-sm-12">
        <span><input type="submit" name="submit" value="Send it" id="submit" /></span>
    </div>
    <div id="alert" class="col-sm-12"></div>
</form>

This is the php code :这是php代码

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'kreso.galic8@gmail.com';
$subject = 'Hello';
$human = $_POST['human'];

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

if ($_POST['submit']) {
    if ($name != '' && $email != '') {
        if ($human == '4') {
            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 if ($_POST['submit'] && $human != '4') {
            echo '<p>You answered the anti-spam question incorrectly!</p>';
        }
    } else {
        echo '<p>You need to fill in all required fields!!</p>';
    }
}
?>

It seems that if ($_POST['submit']) { is not working.似乎if ($_POST['submit']) {不起作用。

You should use empty or isset :您应该使用emptyisset

if (!empty($_POST['submit'])) {

or或者

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

Be careful, if the user press enter in an input field instead of clicking on the submit button, the submit value won't be send.请注意,如果用户在输入字段中按 Enter 而不是单击提交按钮,则不会发送提交值。

You could use a hidden field instead:您可以改用隐藏字段:

<input type="hidden" name="ok" value="1" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM