我制作了一个联系人表单,我希望当用户发送邮件时,出现一条“您的邮件已被发送”的消息,而不是联系人表单,如何修复它?
这里是我的代码
<form method="post" action="" name="contact">
<div class="column">
<input name="name" id="name" placeholder="name" value=""/>
</div>
<div class="column-2">
<input name="email" id="email" placeholder="mail" value="" />
</div>
<div class="column-3">
<textarea id="message" placeholder="Your message" name="message" title="votre message" ></textarea>
</div>
<div class="button">
<span><input class="submit" id="submit" name="submit" type="submit" value="ENVOYER"></span>
</div>
</form>PHP代码
<?php
if(!empty($_POST['name'])&&!empty($_POST['email'])&&!empty($_POST['message']))// check if everything has been filled out before doing anything else
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: test Contact';
$to = 'test@test.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>
<span id="success">OK</span>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
}
}
?>发布于 2015-08-23 09:49:07
您必须使用AJAX。然后从AJAX调用php代码(您需要将php放在一个单独的文件夹中)
发布于 2015-08-23 10:05:31
你有三个选择。
发布于 2015-08-23 10:06:11
如果您想要获得它,那么您必须将成功或失败的页面重定向到另一个页面。我还稍微改变了您的代码结构,因为您需要首先检查表单是否提交。
这是一个有用的例子。
index.php页面
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message'])) {// check if everything has been filled out before doing anything else
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: test Contact';
$to = 'test@test.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($name != '' && $email != '') {
if (mail($to, $subject, $body, $from)) {
header("Location: result.php?success=1");
} else {
header("Location: result.php?success=0");
}
} else {
echo "Name and Email not found";
}
} else {
echo "Please fill up all fields";
}
}
?>
<form method="post" action="" name="contact">
<div class="column">
<input name="name" id="name" placeholder="name" value=""/>
</div>
<div class="column-2">
<input name="email" id="email" placeholder="mail" value="" />
</div>
<div class="column-3">
<textarea id="message" placeholder="Your message" name="message" title="votre message" ></textarea>
</div>
<div class="button">
<span><input type="submit" class="submit" id="submit" name="submit" value="ENVOYER"></span>
</div>
</form>result.php页面
<?php
if (isset($_GET['success']) && $_GET['success'] == 1) {
echo '<p>Your message has been sent!</p><span id="success">OK</span>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
?>https://stackoverflow.com/questions/32165130
复制相似问题