重定向后在页面上显示通知


Showing notification on page after redirect

重定向后,在页面上显示通知时遇到一些问题。我有一个action="ProcessForm.php"的表单,在ProcessForm.php上,我使用重定向回带有该表单的页面

window.location = 'http://www.sample.php#success';

然后在sample.php页面上,我创建了一个if语句:

if(window.location.hash == 'http://www.sample.php#success')
{ echo"<div class="notification success">
    <span></span>
        <div class="text">
        <p><strong>Success!</strong>Form Submitted Successfully!</p>
        </div>
     </div>"

但当我被重定向到那个页面时,没有任何通知。我做错什么了吗?

echo是一个php输出函数,在javascript中无效。除此之外,window.location.hash只会给你#,所以它等于"#成功"。

尝试使用document.write(<<htmlhere>>);

似乎需要一个简单的php解决方案:

ProcessForm.php

$notice = urlencode('<div class="notification success">
<span></span>
    <div class="text">
    <p><strong>Success!</strong>Form Submitted Successfully!</p>
    </div>
 </div>');
header('Location: form.php?notice='.$notice);

form.php

if( !empty($_GET['notice']) ){
echo $_GET['notice'];
}