PHP注释表单不解析所有字段

时间:2014-06-20 11:55:20

标签: php html forms comments

我知道这是基本的,但我在我的网站上使用了php评论表,但它并没有向我发送输入到这些字段的所有信息,请有人帮助我。

这是我使用的HTML:

<form method="post" action="assets/php/mail.php">
    <label>Name</label>
    <input name="name" placeholder="Type Here">
    <label>Email</label>
    <input name="email" type="email" placeholder="Type Here">
    <label>Postcode</label>
    <input name="postcode" placeholder="Type Here">
    <label>Child's Date of Birth</label>
    <input name="dob" placeholder="Type Here">
    <label>Year your child will begin primary school in Reception class</label>
    <input name="year" placeholder="Type Here">
    <label>I would select York Community Free School as first choice for my child</label>
    <input name="send" placeholder="Type Here">
    <label>Message</label>
    <textarea name="message" placeholder="Type Here"></textarea>
    <label>*What is 2+2? (Anti-spam)</label>
    <input name="human" placeholder="Type Here">
    <div class="form-button">
        <input id="submit" name="submit" type="submit" value="Submit">
    </div>
</form>

这是我使用的PHP:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$postcode = $_POST['postcode'];
$dob = $_POST['dob'];
$year = $_POST['year'];
$send = $_POST['send'];
$from = 'From: York Free School Comment form'; 
$to = 'yorkfreeschool@gmail.com'; 
$subject = 'Hello';
$human = $_POST['human'];

$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, go back and try again!</p>'; 
} 
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}

&GT;

所有通过电子邮件发送给我的都是姓名,电子邮件和消息,我做错了什么?请社区帮助。

3 个答案:

答案 0 :(得分:0)

您需要将电子邮件字段添加到$ body变量中以便发送它我在此示例中仅添加了DOB和邮政编码

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$postcode = $_POST['postcode'];
$dob = $_POST['dob'];
$year = $_POST['year'];
$send = $_POST['send'];
$from = 'From: York Free School Comment form'; 
$to = 'yorkfreeschool@gmail.com'; 
$subject = 'Hello';
$human = $_POST['human'];

//Look here
$body = "From: $name\n E-Mail: $email\n Message:\n $message Postcode: \n $postcode DOB: \n $dob \n";

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, go back and try again!</p>'; 
} 
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}

答案 1 :(得分:0)

您没有使用任何您正在抓取的其他字段。如果要包含它们,则需要将它们附加到消息中。

答案 2 :(得分:0)

<!-- ur html code is not valid, look at submit input -->
<form method="post" action="assets/php/mail.php">
    <label>Name</label>
    <input name="name" placeholder="Type Here">
    <label>Email</label>
    <input name="email" type="email" placeholder="Type Here">
    <label>Postcode</label>
    <input name="postcode" placeholder="Type Here">
    <label>Child's Date of Birth</label>
    <input name="dob" placeholder="Type Here">
    <label>Year your child will begin primary school in Reception class</label>
    <input name="year" placeholder="Type Here">
    <label>I would select York Community Free School as first choice for my child</label>
    <input name="send" placeholder="Type Here">
    <label>Message</label>
    <textarea name="message" placeholder="Type Here"></textarea>
    <label>*What is 2+2? (Anti-spam)</label>
    <input name="human" placeholder="Type Here">
    <!--</div> i think that closing div tag should go after input with id == "submit" -->
    <div class="form-button">
    <input id="submit" name="submit" type="submit" value="Submit">
    </div> <!-- here -->
</form>