PHP表单提交验证&引导问题

时间:2015-06-01 13:11:33

标签: php forms validation

我是PHP和bootstrap的新手,几乎所有东西。我一直试图遵循几个关于使用bootstrap制作表单并使用PHP通过电子邮件发送表单的教程。我得到了部分工作而其他部分没有工作。我一直试图做的是让表单在每个表单元素下显示错误消息,如果参数没有得到满足。然后,一旦所有内容都正确填写,表单就会提交而不刷新页面,但会显示成功消息。我已经合并了几个教程,但这个是我试图用错误和成功消息模拟的教程。 https://bootstrapbay.com/blog/working-bootstrap-contact-form/

目前,表单提交并通过电子邮件发送,所有内容都已正确填写,但加载了.php文件,并在空白屏幕上。如果没有输入名称,那么它不会发送电子邮件,仍然会加载带有空白屏幕的.php文件。任何帮助,将不胜感激!下面是我的PHP和HTML片段

HTML



<form name="adoptionForm" method="post" action="phpForms/adoption_doc.php" class="form-horizontal" role="form">
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">Name</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="name" name="name" placeholder="Your Name">
                <?php echo "<p class='text-danger'>$errName</p>";?>
            </div>
        </div>
        <div class="form-group">
            <label for="email" class="col-sm-2 control-label">Email</label>
            <div class="col-sm-10">
                <input type="email" class="form-control" id="email" name="email" placeholder="Your Email">
                <?php echo "<p class='text-danger'>$errEmail</p>";?>
            </div>
    <div class="form-group">
        <div class="col-lg-offset-2 col-lg-10">
            <button type="submit" class="btn btn-default">
                Send Message
            </button>
        </div>
    </div>
</form>
&#13;
&#13;
&#13;

PHP

&#13;
&#13;
<?php
/* Set e-mail recipient */
$myemail = "stackoverflowTest@gmail.com";

/* POSTS INFO */
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];


/* Let's prepare the message for the e-mail */

$subject = "$name has sent you a message";

$message = "

Someone has sent you a message using your contact form:

Name: $name
Email: $email
Address: $address
City: $city
State: $state

";

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

/* Send the message using mail() function */
if (!$errName && !$errEmail) {
    if (mail ($myemail, $subject, $message)) {
        $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>';
    }
}
    

?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我测试了这段代码,它是您尝试使用的代码的更新副本。

您需要将表单上的操作更改为您网页的任何名称。

<?php
if ($_POST["submit"]) {
    $name = $_POST['name'];
    $email = $_POST['email'];

    $to = 'stackoverflowTest@gmail.com';
    $subject = "$name has sent you a message";
    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    } else {

    }

    // 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';
    } else {
        $from = $email;
    }

    // set body of message to be sent

    $message = "
        Someone has sent you a message using your contact form:

        Name: $name
        Email: $email
        Address: $address
        City: $city
        State: $state
        ";

    //Check if address has been entered
    if (!$_POST['address']) {
        $errAddress = 'Please enter your street address';
    } else {
        $address = $_POST['address'];
    }

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

    //Check if state has been entered
    if (!$_POST['state']) {
        $errState = 'Please enter your state';
    } else {
        $address = $_POST['state'];
    }

// If there are no errors, send the email
    if (!$errName && !$errEmail && !$errAddress && !$errCity && !$errState) {
        if (mail($to, $subject, $message, $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>';
        }
    } else {
        $result = '<div class="alert alert-danger">Error in checking.</div>';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="Bootstrap contact form with PHP example by BootstrapBay.com.">
        <meta name="author" content="BootstrapBay.com">
        <title>Adoption Form</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3">
                    <h1 class="page-header text-center">Adoption Form</h1>
                    <form class="form-horizontal" role="form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
                        <div class="form-group">
                            <label for="name" class="col-sm-2 control-label">Name</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                                <?php echo "<p class='text-danger'>$errName</p>"; ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="email" class="col-sm-2 control-label">Email</label>
                            <div class="col-sm-10">
                                <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
                                <?php echo "<p class='text-danger'>$errEmail</p>"; ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="address" class="col-sm-2 control-label">Street Address</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="address" name="address" placeholder="Your Street Address">
                                <?php echo "<p class='text-danger'>$errAddress</p>"; ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="city" class="col-sm-2 control-label">City</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="city" name="city" placeholder="Your City">
                                <?php echo "<p class='text-danger'>$errCity</p>"; ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="state" class="col-sm-2 control-label">State</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="state" name="state" placeholder="Your State">
                                <?php echo "<p class='text-danger'>$errState</p>"; ?>
                            </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">
                                <?php echo $result; ?>  
                            </div>
                        </div>
                    </form> 
                </div>
            </div>
        </div>   
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
    </body>
</html>