PHP邮件功能无法点击

时间:2016-04-12 01:29:03

标签: php email onclick

我试图创建一个简单的表单,填写后会向我发送一封特定的电子邮件。该网站肯定注册了点击并告诉我我的消息已发送,但没有电子邮件来。

但是,如果我摆脱if语句,它会向我发送一封电子邮件,但只有当我刷新页面时,它才会向我发送任何相关信息。所以,我的if语句是错误的,但当我将其更改为strlen($name)!=0时,它仍然没有向我发送电子邮件。

我认为问题在于,当我点击按钮时,它不会运行php代码。我该如何解决?谢谢。

这是我的代码:

<form method="post" id="contactForm" name="sentMessage" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="row">
    <div class="col-md-6">
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Your Name *" id="name" required data-validation-required-message="Please enter your name.">
            <p class="help-block text-danger"></p>
        </div>
        <div class="form-group">
            <input type="email" class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address.">
            <p class="help-block text-danger"></p>
        </div>
        <div class="form-group">
            <input type="tel" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">
            <p class="help-block text-danger"></p>
        </div>
    </div>
    <div class="col-md-6">
        <div class="form-group">
            <textarea class="form-control" placeholder="Your Message *" id="message" required data-validation-required-message="Please enter a message."></textarea>
            <p class="help-block text-danger"></p>
        </div>
    </div>
    <div class="clearfix"></div>
    <div class="col-lg-12 text-center">
        <div id="success"></div>
        <button type="submit" id="submit" class="btn btn-xl">Send Message</button>
    </div>
</div>
 <?php
    $name=$_POST["name"];
    $email=$_POST["email"];
    $phone=$_POST["phone"];
    $message=$_POST["message"];
    if(isset($_POST["submit"])){
        $message=$message."\nSent by".$name." whose phone number is ".$phone."and whose email is".$email;
        mail("myemail@gmail.com","title","let's try this message.".$message);
    }
?>

1 个答案:

答案 0 :(得分:3)

$_POST数据元素使用name属性,而不是id属性;将表单中的所有id更改为name

例如:

<input type="tel" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">

应该是:

<input type="tel" class="form-control" placeholder="Your Phone *" name="phone" required data-validation-required-message="Please enter your phone number.">
                                                                  ^^^^

现在,$_POST["phone"]应该得到正确的值。对所有其他值执行此操作。

有关详细信息,请参阅this SO answer