如何获取PHP表单页面以关闭成功提交? [英] How do I get a PHP form page to close on succesful submit?
问题描述
有没有一种简单快捷的方法可以让这个form.php代码在提交后关闭窗口而不是重定向?如果没有办法做到这一点,我可以处理重定向,但希望窗口刚刚关闭,因为原始窗体将在新窗口中打开。代码如下:
Is there a quick and easy way to make this form.php code close the window after submitting instead of redirecting? I can deal with the redirect if there is no way to do this, but would like to have the window just close, as the original form is being opened in a new window. Code below:
<?php
if (!empty($_POST)) {
// Used for later to determine result
$success = $error = false;
// Object syntax looks better and is easier to use than arrays to me
$post = new stdClass;
// Usually there would be much more validation and filtering, but this
// will work for now.
foreach ($_POST as $key => $val)
$post->$key = trim(strip_tags($_POST[$key]));
// Check for blank fields
if (empty($post->First) OR empty($post->Last) OR empty($post->Email))
$error = true;
else {
// Get this directory, to include other files from
$dir = dirname(__FILE__);
// Get the contents of the HTML email into a variable for later
ob_start();
require_once($dir.'/html.php');
$html_message = ob_get_contents();
ob_end_clean();
// Load the SwiftMailer files
require_once($dir.'/swift/swift_required.php');
$mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer
$message = Swift_Message::newInstance()
->setSubject('Hospitality Recycling Program Calculator Results') // Message subject
->setTo(array('bsmith@cleantheworld.org', $post->Email =>$post-> First )) // Array of people to send
->setFrom(array('bsmith@cleantheworld.org' => 'Clean the World')) // From:
->setBody($html_message, 'text/html');
// Send the email, and show user message
if ($mailer->send($message))
$success = true;
else
$error = true;
}
}
header("location: http://www.cleantheworld.org/newpartner.asp");
?>
推荐答案
我同意@jeroen,更好的解决方案是使用AJAX,但您可以在重定向的头部输出一些javascript来关闭页面。再次,我不建议这是一个很好的解决方案,但只要在用户浏览器中启用javascript,它就会工作。
I agree with @jeroen that a better solution would be to use AJAX but you can just output some javascript in the head of the redirect to close the page. Again I don't recommend this as a good solution to the problem but it will work as long as javascript is enabled in the users browser.
<script type="text/javascript>
self.close();
</script>
这篇关于如何获取PHP表单页面以关闭成功提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!