我有一个php邮件表单(工作良好),目前重定向到一个感谢页面。我正在改造网站,并希望(而不是重定向到另一个页面)有(在成功提交)一个模态弹出窗口出现,并说谢谢。我还需要清除表单,因为用户将停留在当前页面上。我已经搜索(既在stackoverflow和网络搜索)的解决方案。我找到了一些"想法",但都没有奏效。我目前使用模态弹出窗口在网站上的其他事情(自改造),一般了解他们是如何工作的。我对php也有基本的了解。我在想,如果它可能(?)有一些类型的javascript在头调用模态弹出。我也看到过需要AJAX的想法。
任何帮助都将是非常感激的。
这是我的html for form:<p class="formrequired">Required fields are marked with an asterisk(*)</p>
<form action="form.php" method="post">
<ol class="forms">
<li><label for="first_name">*First Name</label><input type="text" name="first_name"/></li>
<li><label for="last_name">Last Name</label><input type="text" name="last_name"/></li>
<li><label for="email">*Email</label><input type="text" name="email"/></li>
<li><label for="comment">*Message</label><textarea name="comment"cols="45" rows="5"></textarea></li>
<li><input type="submit" value="Submit" class="submit"/>
</ol>
</form>
和我的php:
<?php
$myemail = "person@email.com";
$first_name = check_input($_POST['first_name'], "Please enter your first name");
$last_name = check_input($_POST['last_name']);
$email = check_input($_POST['email']);
$comment = check_input($_POST['comment'], "Please enter your message");
if (!preg_match("/(['w'-]+'@['w'-]+'.['w'-]+)/", $email))
{
show_error("E-mail address is not a valid email address");
}
$subject="New form submitted on Napoleonville Fire";
$message = "Hello Jordy!
Your contact form has been submitted by:
First Name: $first_name
Last Name: $last_name
E-mail: $email
They left the following message:
$comment
End of message.
";
mail($myemail, $subject, $message);
header('Location: thankyou.html');
exit();
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
再次感谢所有人的帮助。
如果这个问题可以解决,我将有一个"part 2"。
我能想到的两个流程
非ajax
将表单提交给服务器。然后,在读取成功页面时,呈现显示模态的页面。提供某种关闭按钮,将其设置为none,或者将其从DOM中完全删除。在这种方法中,您可以使用PHP处理空表单的呈现。这是比较传统的范例。
为此,向表单提交添加一个事件处理程序。通过AJAX提交表单,但不要立即清除表单字段。等待服务器的响应。例如,如果服务器在表单提交中发现验证错误,则必须更新表单以反映此错误。如果服务器返回成功响应,用Javascript清除表单字段并显示模态。