我只关注this tutorial如何使用PHP创建联系表单。现在一切正常,但是当我提交表单时,它会在新的空白页面上返回一条消息。我希望这发生在联系表单下面的当前页面上。
这是我第一次使用PHP做任何事情,所以我不知道如何做到这一点。在本教程中,简要提到您可以将脚本放在任何您希望显示消息的位置,但它似乎对我没用。
这是我的HTML代码:
<form method="post" action="contact.php">
<label>Name</label>
<input name="name" placeholder="John Doe">
<label>Email</label>
<input name="email" type="email" placeholder="john@doe.com">
<label>Message</label>
<textarea name="message" placeholder="Hello..."></textarea>
<label id="antispam">What is 2+2? (Anti-spam)</label>
<input id="antispambox" name="human" placeholder="4">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
这是我的PHP代码:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'contact@tangledindesign.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
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 fields!!</p>';
}
}
?>
我已经找到了一些答案,但对我来说都没有任何意义。我也试过检查教程网站上的例子,但是,当然我无法访问PHP。
有人可以向我解释一下吗?
答案 0 :(得分:2)
如果您希望在同一页面上显示该消息,则必须将php代码放在同一页面上,如下所示:
<form method="post" action="">
<label>Name</label>
<input name="name" placeholder="John Doe">
<label>Email</label>
<input name="email" type="email" placeholder="john@doe.com">
<label>Message</label>
<textarea name="message" placeholder="Hello..."></textarea>
<label id="antispam">What is 2+2? (Anti-spam)</label>
<input id="antispambox" name="human" placeholder="4">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
<?php
if(!empty($_POST['name'])&&!empty($_POST['email'])&&!empty($_POST['message'])&&!empty($_POST['human']))// check if everything has been filled out before doing anything else
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'contact@tangledindesign.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
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 fields!!</p>';
}
}
}
?>
答案 1 :(得分:0)
从你看起来要求你需要研究AJAX。您需要使用jQuery之类的JavaScript库来简化它。它将完全符合您的要求。将表单数据发布到“contact.php”并将返回的消息(成功/错误)直接发送回您当前所在的页面。没有重定向,没有刷新。
答案 2 :(得分:0)
有不同的方法可以解决这个问题。 AJAX就是其中之一,但是因为你刚开始我的意见是避免这种情况 - 首先学习基础知识。下面的代码就是 - 基础知识。
此方法需要在同一页面上进行表单和处理。这通常不是最好的方法,但它可能是最简单的 - 而且我的观点是,当你开始时,简单就是你的朋友。
contact.php
<?php
// First, check if the submit button has been pressed - no processing occurs unless the form has been submitted.
// This is necessary because your $_POST variables do not exist until the form is submitted.
if (isset($_POST['submit'])) {
// The button has been pressed, so now gather data and set variables
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'contact@tangledindesign.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// Now, validate and process
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 {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all fields!!</p>';
}
}
?>
<form method="post" action="contact.php">
<label>Name</label>
<input name="name" placeholder="John Doe">
<label>Email</label>
<input name="email" type="email" placeholder="john@doe.com">
<label>Message</label>
<textarea name="message" placeholder="Hello..."></textarea>
<label id="antispam">What is 2+2? (Anti-spam)</label>
<input id="antispambox" name="human" placeholder="4">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
请记住,虽然这种方法有效,但从长远来看,这不是最佳方法。最好将逻辑和标记分开。这意味着至少有两个页面 - 一个显示表单,另一个页面处理表单。更好的方法将涉及面向对象的编程。但是,我坚定地相信准备,消防,目标方法......当你试图学习,让它先工作,后来担心最佳实践和优化。当您遇到更大的问题并且需要更多的复杂性时,您可以进行调整并找出更好的方法来做事。进步只来自实践,试验和错误。祝你好运。