我想让我的个人网站更新,不能弄清楚为什么我的联系方式不工作。以下是问题页面的url: 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非常陌生,所以除了填写表单之外,我不知道如何测试它。
非常感谢任何帮助。我非常慌乱,需要把这个网站建起来。谢谢你的帮助。
您的表单是通过JavaScript生成的,但是从一个普通的HTML表单开始将帮助您更快地找出步骤。通过JavaScript进行客户端验证很好,但您也需要服务器端PHP验证。您需要加强表单处理(别担心,我也是!)。你会在这个网站上听到关于注入攻击之类的抨击。如果可以的话,您可能想要研究一些关于web应用程序安全性的书籍。这里有一些可以帮助你入门的书。
PHP安全Web应用程序安全
您需要一个<form>
标签,其中包含以下内容。
<form method="post" action="contact.php">
我找到你的开放<form>
标签。有趣。
<form class="contactform" role="form" method="post" action="#/email.php">
您可能想要调查您的action="#/email.php"
属性多一点。本来就是这样的吗?
我通常这样做(HTML 4.01 PHP内部。您正在使用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编程,第3版
PHP开始PHP, MySQL, &Apache:第5版
PHP教程第2版
PHP and MySQL Web Development:第4版
学习PHP, MySQL, JavaScript和CSS:第二版
而且,网上有很多免费的教程!
除非我错过了什么奇怪的东西:
<form class="contactform" role="form" method="post" action="#/email.php">
action
属性在URL前面有一个#
。你的网站上没有这样的URL,而#实际上有点破坏了它。结果请求的形式实际上去你的/
;所以email.php
永远不会被调用。
你可能想用action="/email.php"
代替。