在页面加载时从一个页面转发到另一个页面时在网页中显示警报消息 [英] Display alert msg in web page when forwarding from one page to another on page load
问题描述
我在 php 中创建了一个 html 页面,提交后我使用 PHP 验证了该页面.验证后,我想显示一个警报消息以显示其状态,例如显示任何问候语或重新输入的请求.
I have created a html page in php and upon submission i validates that page using PHP. After validating i want to show an alert msg to show its status like showing any greeting or request for re-enter.
我没有验证.现在我正在使用
I have dont validation. Now i m using
header( 'Location: http://localhost/assignment/WebForm.htm' ) ;
将用户重定向到同一页面,但在页面加载时会出现警告信息或类似内容.我需要做什么?
to redirect user to same page but with a alert msg at page load or something like that. What I need to do ?
推荐答案
当你使用header
时,你无法在文档正文中输出任何东西,使得任何alert()
编码>不可能.
When you use header
, you can't output anything in the document's body, making any alert()
ing impossible.
实现这一目标的一个常用技巧是将 alert()
委托给目标页面:
A often used trick to achieve this is to delegate the alert()
ing to the target page:
header( 'Location: http://localhost/assignment/WebForm.htm?alert='.
urlencode("Hello!")) ;
然后在 WebForm.htm 中:
and then in WebForm.htm:
<?php if (isset($_GET["alert"])): ?>
<script type="text/javascript">
alert("<?php echo htmlentities(urldecode($_GET["alert"])); ?>");
</script>
<?php endif; ?>
只要记住在输出消息时htmlentities()
输出即可.
just remember to htmlentities()
the output when outputting the message.
如果您已经在使用会话,为了 100% 安全和优雅的 URL,您还可以使用 rand
在 PHP 中生成一个随机密钥,将消息存储在 $_SESSION["message_$randomKey"]
并在 GET 请求中传递密钥.这样,用户在 URL 中看到的唯一内容就是密钥,而不是消息.
If you are already using sessions, for 100% security and elegant URLs, you could also generate a random key in PHP using rand
, store the message in $_SESSION["message_$randomKey"]
and pass the key in the GET request. That way, the only thing the user sees in the URL is the key, and not the message.
这篇关于在页面加载时从一个页面转发到另一个页面时在网页中显示警报消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!