联系表格在我的网站上无效

时间:2014-05-30 17:13:59

标签: php html contact-form

我正在尝试更新我的个人网站,无法弄清楚我的联系表单无效的原因。以下是包含问题的页面的网址:http://zachjanice.com/#/contact

当我填写表格时,没有确认或拒绝发生。在我的contact.php文件中,我有以下代码:

<div class="midnight-blue">
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2 text-center animated fadeIn">
                <h1 class="page-header">Contact</h1>
                <p>Drop me a line and I will respond as quickly as possible.<br> Thank you for your interest.</p>
            </div><!-- col-md-8 -->
        </div><!-- row -->
    </div><!-- container -->
</div><!-- midnight-blue -->
<div class="clouds">
    <div class="container">
        <div class="row" id="contact">
            <div class="col-md-8 col-md-offset-2 well">
                <form class="contactform" role="form" method="post" action="#/email.php">
                    <div class="form-group">
                        <label class="control-label">Name</label>
                        <div class="controls">
                            <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-user"></i></span>
                                <input type="text" class="form-control" name="name" placeholder="Name" required>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label">Email</label>
                        <div class="controls">
                            <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-envelope"></i></span>
                                <input type="text" class="form-control" id="email" name="email" placeholder="Email" required>
                            </div>
                        </div>  
                    </div>

                    <div class="form-group ">
                        <label class="control-label">Message</label>
                        <div class="controls">
                            <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-pencil"></i></span>
                                <textarea name="message" class="form-control " rows="4" cols="78" placeholder="Enter your message here" required></textarea>

                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label">What is 2+2?</label>
                        <div class="controls">
                            <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-question"></i></span>
                                <input type="text" class="form-control" id="human" name="human" placeholder="Are you human?" required>
                            </div>
                        </div>  
                    </div>

                    <div class="controls" style="margin-left: 40%;">

                        <button type="submit" name="submit" id="mybtn" class="btn btn-primary">Send Message</button>

                    </div>
                </form>
                <?php
                    if ($_POST['submit']) {
                        if ($name != '' && $email != '') {
                            if ($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>';
                        }
                        } else {
                            echo '<p>You need to fill in all required fields!!</p>';
                        }
                    }
                ?>
            </div>
        </div><!-- row -->
    </div><!-- container -->
</div><!-- clouds -->

在我的email.php文件中,我有以下内容:

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: zachjanice.com';
$to = 'zachjanice57@gmail.com';
$subject = 'From zachjanice.com';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

?>

我是php的新手,所以除了填写表单外,我不知道如何测试它。

非常感谢任何帮助。我慌张不已,需要建立这个网站。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

您的表单是通过JavaScript生成的,但从纯HTML表单开始将帮助您更快地计算出步骤。通过JavaScript进行客户端验证很好,但您也需要服务器端PHP验证。你需要加强表格处理(不用担心,我也是!)。你会在这个网站上看到关于注射攻击等问题。如果可以,您可能需要调查一些有关Web应用程序安全性的书籍。以下是一些可以帮助您入门的书籍。

PHP Security

Web Application Security

您需要一个<form>标记,其中包含以下内容。

<form method="post" action="contact.php">

我找到了您的<form>开头标记。有趣。

<form class="contactform" role="form" method="post" action="#/email.php">

您可能需要更多地调查action="#/email.php"属性。这是它应该是什么?

我通常做这样的事情(PHP内部的HTML 4.01。你使用的是HTML5)(这行是通过PHP生成的)。注意:不完整的例子。

<form id="contactForm" name="contactForm" enctype="application/x-www-form-urlencoded" accept-charset="UTF-8" action="contact.php" method="post">

</form>

为了使以下代码正常工作,必须先调用代码类似 email.php ,以便从$_POST超全局中提取值数组并存储在您在contact.php($ name,$ email,$ human,$ to,$ subject,$ body,$ from)中使用的变量中。否则,您的表格将像您说的那样无法与您联系。这是一个逻辑错误,但如果您找到一个好的例子,您可以立即修复它。

没有承诺,但试试这个:

<?php

    require('email.php');  //Assuming it is in the same directory as your contact.php

    if ($_POST['submit']) {
        if ($name !== '' && $email !== '') {  //Variables have not been extracted from $_POST yet, as in email.php
            if ($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>'; 
            } 
        } elseif ($_POST['submit'] && $human !== '4') {
            echo '<p>You answered the anti-spam question incorrectly!</p>';
        } else {
            echo '<p>You need to fill in all required fields!!</p>';
        }
    }
?>

我可能会建议一些资源来加强表单处理。

PHP Programming, 3rd Edition

Beginning PHP

PHP, MySQL, & Apache: 5th Edition

PHP Cookbook, 2nd Edition

PHP and MySQL Web Development: 4th Edition

Learning PHP, MySQL, JavaScript, and CSS: Second Edition

此外,网上有很多免费教程!

答案 1 :(得分:0)

除非我遗漏了一些奇怪的东西:

<form class="contactform" role="form" method="post" action="#/email.php">

action属性在网址前面有#。您的网站上没有此类网址,#实际上会打破它。该表单的结果请求实际上发送到您的/;所以永远不会调用email.php

您可能打算改为action="/email.php"

相关问题