简体   繁体   English

使用模式弹出窗口进行PHP重定向

[英]using modal popup for php redirect

I have a php mail form (working fine) that currently redirects to a thank you page. 我有一个php邮件表单(工作正常),当前可以重定向到“谢谢”页面。 I am revamping the site and would like to (instead of redirecting to another page) have (upon successful submit) a modal popup appear and say thank you. 我正在改造网站,并希望(而不是重定向到另一个页面)(成功提交后)出现一个模式弹出窗口,并说谢谢。 I would also need to have the form cleared since the user would be staying on the current page. 我还需要清除该表单,因为用户将停留在当前页面上。 I have searched (both on stackoverflow and web searches) for a solution. 我已经搜索(包括关于stackoverflow和Web搜索)的解决方案。 I have found a few "ideas" but nothing that has worked. 我发现了一些“想法”,但没有任何效果。 I am currently using modal popups on other things on the site (since revamp) and generally understand how they work. 我目前在网站上的其他内容上使用模式弹出窗口(自改版以来),并且通常了解它们的工作方式。 I also have a basic understanding of php. 我对php也有基本的了解。 I was thinking if its possible(?) to have some type of javascript in header to invoke the modal popup. 我在考虑是否有可能在标头中使用某种类型的JavaScript来调用模式弹出窗口。 I've also seen ideas of it needing to be AJAX. 我也已经看到需要使用AJAX的想法。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Here's my html for form: 这是我的HTML表格:

<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>

and my php: 和我的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();
}


?>

Again, thanks for any and all help. 再次感谢您提供的所有帮助。

I will have a "part 2" if this can be solved. 如果可以解决,我将有一个“第2部分”。

Two flows I can think of 我能想到的两个流程

Non-AJAX 非AJAX

Submit your form to the server as you currently do. 像现在一样将表单提交到服务器。 Then when redering the success page, render the page with the modal showing. 然后,在重新显示成功页面时,渲染带有模式显示的页面。 Provide a close button of some sort that set's display none on it or deletes it from the DOM altogether. 提供某种关闭按钮,该按钮在其上不显示任何内容或将其完全从DOM中删除。 In this approach you can handle rendering the empty form with PHP. 通过这种方法,您可以使用PHP处理呈现空表单。 This is the more traditional paradigm. 这是更传统的范例。

AJAX AJAX

For this, add an event handler to the form submission. 为此,将事件处理程序添加到表单提交中。 Submit the form via AJAX, but don't immediately clear the form fields. 通过AJAX提交表单,但不要立即清除表单字段。 Wait for the response from the server. 等待服务器的响应。 If the server finds a validation error in the form submission for example you will have to update the form to reflect this error. 例如,如果服务器在表单提交中发现验证错误,则必须更新表单以反映此错误。 If the server returns a success response, clear the form fields with Javascript and show the modal. 如果服务器返回成功响应,请使用Javascript清除表单字段并显示模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM