新手网站创建者。无法使表单与php脚本

时间:2017-04-04 13:28:31

标签: php html css forms

我创建了一个网站,我没有在php工作。网站已启动并正在运行,但表单无法正常运行。现在它提交到我的sform.php文件,但只是显示一个空白页面,并且没有提交到$ to部分中的电子邮件(出于隐私原因,我将其替换为redacted@redacted.com)。这是表单代码和php。我相信你们都会立刻知道这个问题究竟是什么。对不起我的无知。

形式:

<form class="form-horizontal" role="form" method="post" action="sform.php">
                      <div class="form-group">
                        <label for="name" class="col-lg-4 control-label"></label>
                        <div class="col-lg-10">
                          <input type="name" class="form-control" id="name" placeholder="Your Name">
                        </div>
                      </div>
                      <div class="form-group">
                        <label for="email" class="col-lg-4 control-label"></label>
                        <div class="col-lg-10">
                          <input type="email" class="form-control" id="email" placeholder="Email">
                        </div>
                      </div>
                        <div class="form-group">
                        <label for="phone" class="col-lg-4 control-label"></label>
                        <div class="col-lg-10">
                          <input type="phone" class="form-control" id="phone" placeholder="Phone">
                        </div>
                      </div>

                        <div class="form-group">
                        <label for="company" class="col-lg-4 control-label"></label>
                        <div class="col-lg-10">
                          <input type="company" class="form-control" id="company" placeholder="Company Name">
                        </div>
                      </div>


    <div class="form-group">
    <label for="message" class="col-lg-4 control-label"></label>
    <div class="col-lg-10">
    <textarea rows="3" class="form-control" id="message" placeholder="What can we do for you?"></textarea>
    </div>
    </div>
                      <div class="form-group">
                        <div class="col-lg-10">
                          <button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-ok-sign"></span>  Submit</button>
                        </div>
                      </div>
                   </form><!-- form -->

=============================================== ==========

PHP:

<?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['comments'];
    $human = intval($_POST['human']);
    $from = 'redacted'; 
    $to = 'redacted@redacted.com'; 
    $subject = 'redacted ';

    $body = "From: $name\n E-Mail: $email\n Comments: $comments\n";

    // 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['comments']) {
        $errMessage = 'Please enter your message';
    }
    //Check if simple anti-bot test is correct
    if ($human !== 5) {
        $errHuman = 'Your anti-spam is incorrect';
    }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
    $result='<div class="alert alert-success">Thank You! We will be in contact with you shortly.</div>';
} else {
    $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again.</div>';
}
}
}
?>

=============================================== ===================

再次感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

我认为你错过了文本框中的name属性。 你的代码:

<input type="name" class="form-control" id="name" placeholder="Your Name">

试试这个:

<input type="text" class="form-control" id="name" name="name" placeholder="Your Name">

在所有输入字段中添加name =&#34;&#34;。

答案 1 :(得分:-1)

只有在未设置POST值的情况下才设置$ err *变量,但无论如何都要测试它们。如果设置了POST值,则它们不存在。通过稍微改变一下,你可能会像:

if (!$_POST['comments']) {
    $errMessage = 'Please enter your message';
} else {
    $errMessage = false;
    $comments = $_POST['comments'];
}

这可能不是最好的方法,但你可以让这个工作,然后随着你的技能进步改进它。