Php 联系人表单在我的网站上不起作用
我正在尝试更新我的个人网站,但无法找出我的联系方式不起作用的原因。以下是问题所在页面的url: 当我填写表格时,没有发生确认或拒绝。在我的contact.php文件中,我有以下代码:Php 联系人表单在我的网站上不起作用,php,html,contact-form,Php,Html,Contact Form,我正在尝试更新我的个人网站,但无法找出我的联系方式不起作用的原因。以下是问题所在页面的url: 当我填写表格时,没有发生确认或拒绝。在我的contact.php文件中,我有以下代码: <div class="midnight-blue"> <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2 text-c
<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 -->
接触
给我写信,我会尽快回复。
谢谢您的关注
名称
电子邮件
消息
什么是2+2?
发送消息
在根目录中的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非常陌生,所以除了填写表单外,我不知道如何测试它
非常感谢您的帮助。我非常慌乱,需要建立这个网站。谢谢你的帮助。除非我错过了一些奇怪的东西:
<form class="contactform" role="form" method="post" action="#/email.php">
action
属性在URL前面有一个#
。你的网站上没有这样的URL,而#实际上有点破坏了它。该表单的结果请求实际上会转到您的/
;因此,email.php
永远不会被调用
您可能打算使用
action=“/email.php”
。您的表单是通过JavaScript生成的,但从简单的HTML表单开始可以帮助您更快地完成步骤。通过JavaScript进行客户端验证很好,但您也需要服务器端PHP验证。你需要加强表单处理(别担心,我也是!)。你会在这个网站上看到关于注射攻击之类的攻击。如果可以的话,您可能需要研究一些关于web应用程序安全性的书籍。这里有一些书可以让你开始学习
您需要一个
标记,其中的某个位置包含以下内容
<form method="post" action="contact.php">
为了使下面的代码正常工作,必须首先调用类似于email.php的代码,以便从$\u 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>';
}
}
?>
我可能会建议一些资源来加强表单处理
此外,网上还有很多免费教程 您是否有权访问可以查看错误日志的服务器?打开所有错误报告,您将能够看到问题所在。如何不工作?不发送电子邮件?表单实际提交到的脚本中没有
mail()
调用,因此html下面的mail()
调用实际上是完全无用的。在我的contact.php页面底部,我在php if(mail($to、$subject、$body、$from))中包含了我所有的html{echo'您的消息已发送!”;这不是此脚本的正确位置吗?我正在尝试遵循此站点的教程:
<form id="contactForm" name="contactForm" enctype="application/x-www-form-urlencoded" accept-charset="UTF-8" action="contact.php" method="post">
</form>
<?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>';
}
}
?>