如何让php联系页面工作

时间:2015-10-09 13:06:36

标签: php

您好,我需要有人可以帮助我使用php,我目前的PHP代码只是带我去找一个" Page not found"页面所以,如果有人能告诉我如何解决这个问题我会很高兴。

这是Html:

<div>
            <div>
                <p class="text-center" style="font-family:Aparajita; font-size:1.5vw; padding:2%;">Drop me a line</p>
            </div>

    <form class="form-horizontal bu_contact col-lg-4" role="form" method="post" action="index.php">
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label glyphicon glyphicon-user"></label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
            </div>
        </div>
        <div class="form-group">
            <label for="email" class="col-sm-2 control-label glyphicon glyphicon-envelope"></label>
            <div class="col-sm-10">
                <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="">
            </div>
        </div>
        <div class="form-group">
            <label for="message" class="col-sm-2 control-label glyphicon glyphicon-pencil"></label>
            <div class="col-sm-10">
                <textarea class="form-control" rows="4" name="message" placeholder="Your Message"></textarea>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-10 col-sm-offset-2">
                <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-10 col-sm-offset-2">
            </div>
        </div>
    </form>
    </div>

php代码:

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $human = intval($_POST['human']);
        $from = 'Demo Contact Form'; 
        $to = 'lars81wolf@gmail.com'; 
        $subject = 'Message from Contact Demo ';

        $body ="From: $name\n E-Mail: $email\n Message:\n $message";
        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }

// If there are no errors, send the email
if (!$errName &#038;& !$errEmail &#038;& !$errMessage &#038;& !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='

<div class="alert alert-success">Thank You! I will be in touch</div>


';
    } else {
        $result='

<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>


';
    }
}
    }
?>

1 个答案:

答案 0 :(得分:0)

我会把它放在一个文件名中,它与目标基本相同,一切都会好的。

<?php
// NAME THIS FILE contact.php so that the post will send it to this file
if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $human = intval($_POST['human']);
        $from = 'Demo Contact Form'; 
        $to = 'lars81wolf@gmail.com'; 
        $subject = 'Message from Contact Demo ';

        $body ="From: $name\n E-Mail: $email\n Message:\n $message";
        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }

    // If there are no errors, send the email
    if (!$errName &#038;& !$errEmail &#038;& !$errMessage &#038;& !$errHuman) {
        if (mail ($to, $subject, $body, $from)) {
        echo '

                <div class="alert alert-success">Thank You! I will be in touch</div>


            ';
        } else {
        echo '

                <div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>


            ';
        }
    }
} else {
// Put all your stuff that will be the normal page in here
?>
<div>
            <div>
                <p class="text-center" style="font-family:Aparajita; font-size:1.5vw; padding:2%;">Drop me a line</p>
            </div>

    <form class="form-horizontal bu_contact col-lg-4" role="form" method="post" action="contact.php">
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label glyphicon glyphicon-user"></label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
            </div>
        </div>
        <div class="form-group">
            <label for="email" class="col-sm-2 control-label glyphicon glyphicon-envelope"></label>
            <div class="col-sm-10">
                <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="">
            </div>
        </div>
        <div class="form-group">
            <label for="message" class="col-sm-2 control-label glyphicon glyphicon-pencil"></label>
            <div class="col-sm-10">
                <textarea class="form-control" rows="4" name="message" placeholder="Your Message"></textarea>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-10 col-sm-offset-2">
                <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-10 col-sm-offset-2">
            </div>
        </div>
    </form>
    </div>
<?php
}
?> 

但是不要忘记所有HTML符合头部和脚部。

PS:我没有检查任何进一步的问题只是为了例程本身。除非您通过邮件功能正确配置了PHP以使用MTA,否则不确定这是否真的会发送邮件;)