php 按下警报按钮后重定向到页面/URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19825283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect to a page/URL after alert button is pressed
提问by Jben Kaye
i have referred to this two questions call php page under Javascript functionand Go to URL after OK button in alert is pressed. i want to redirect to my index.php after an alert box is called. my alert box is in my else statement. below is my code:
我已经提到了这两个问题在 Javascript 函数下调用 php 页面和在按下警报中的确定按钮后转到 URL。我想在调用警报框后重定向到我的 index.php。我的警告框在我的 else 语句中。下面是我的代码:
processor.php
处理器.php
if (!empty($name) && !empty($email) && !empty($office_id) && !empty($title) && !empty($var_title) && !empty($var_story) && !empty($var_task) && !empty($var_power) && !empty($var_solve) && !empty($var_result)) {
(some imagecreatefromjpeg code here)
else{
echo '<script type="text/javascript">';
echo 'alert("review your answer")';
echo 'window.location= "index.php"';
echo '</script>';
}
it's not displ ying anything(no alert box and not redirecting). when i delet this part echo 'window.location= "index.php"';
it's showing the alert. but still not redirecting to index.php. hope you can help me with this. please dont mark as duplicate as i have made tose posts as reference. thank you so much for your help.
它没有显示任何内容(没有警告框,也没有重定向)。当我删除这部分时,echo 'window.location= "index.php"';
它会显示警报。但仍然没有重定向到 index.php。希望你能帮我解决这个问题。请不要标记为重复,因为我已将这些帖子作为参考。非常感谢你的帮助。
回答by Robbie Averill
You're missing semi-colons after your javascript lines. Also, window.location
should have .href
or .replace
etc to redirect - See this post for more information.
您在 javascript 行之后缺少分号。此外,window.location
应该有.href
或.replace
等重定向 -有关更多信息,请参阅此帖子。
echo '<script type="text/javascript">';
echo 'alert("review your answer");';
echo 'window.location.href = "index.php";';
echo '</script>';
For clarity, try leaving PHP tags for this:
为了清楚起见,请尝试为此保留 PHP 标记:
?>
<script type="text/javascript">
alert("review your answer");
window.location.href = "index.php";
</script>
<?php
NOTE: semi colons on seperate lines are optional, but encouraged - however as in the comments below, PHP won't break lines in the first example here but will in the second, so semi-colons arerequired in the first example.
注意:分隔行上的分号是可选的,但鼓励使用 - 然而,正如下面的评论一样,PHP 不会在第一个示例中换行,但在第二个示例中会换行,因此第一个示例中需要分号。
回答by user2962745
if (window.confirm('Really go to another page?'))
{
alert('message');
window.location = '/some/url';
}
else
{
die();
}
回答by fos.alex
Like that, both of the sentences will be executed even before the page has finished loading.
这样,即使在页面加载完成之前,这两个句子也会被执行。
Here is your error, you are missing a ';' Change:
这是您的错误,您缺少一个 ';' 改变:
echo 'alert("review your answer")';
echo 'window.location= "index.php"';
To:
到:
echo 'alert("review your answer");';
echo 'window.location= "index.php";';
Then a suggestion: You really should trigger that logic after some event. So, for instance:
然后是一个建议:您确实应该在某个事件之后触发该逻辑。因此,例如:
document.getElementById("myBtn").onclick=function(){
alert("review your answer");
window.location= "index.php";
};
Another suggestion, use jQuery
另一个建议,使用jQuery
回答by DevlshOne
window.location = mypage.href
is a direct command for the browser to dump it's contents and start loading up some more. So for better clarification, here's what's happening in your PHP script:
window.location = mypage.href
是浏览器转储其内容并开始加载更多内容的直接命令。所以为了更好的说明,这里是你的 PHP 脚本中发生的事情:
echo '<script type="text/javascript">';
echo 'alert("review your answer");';
echo 'window.location = "index.php";';
echo '</script>';
1) prepare to accept a modification or addition to the current Javascript cache. 2) show the alert 3) dump everything in browser memory and get ready for some more (albeit an older method of loading a new URL (AND NOTICE that there are no "\n" (new line) indicators between the lines and is therefore causing some havoc in the JS decoder.
1) 准备接受对当前 Javascript 缓存的修改或添加。2) 显示警报 3) 转储浏览器内存中的所有内容并为更多内容做好准备(尽管是加载新 URL 的旧方法(并注意行之间没有“\n”(新行)指示符,因此在 JS 解码器中造成一些破坏。
Let me suggest that you do this another way..
让我建议你以另一种方式做到这一点..
echo '<script type="text/javascript">\n';
echo 'alert("review your answer");\n';
echo 'document.location.href = "index.php";\n';
echo '</script>\n';
1) prepare to accept a modification or addition to the current Javascript cache. 2) show the alert 3) dump everything in browser memory and get ready for some more (in a better fashion than before) And WOW - it all works because the JS decoder can see that each command is anow a new line.
1) 准备接受对当前 Javascript 缓存的修改或添加。2) 显示警报 3) 转储浏览器内存中的所有内容并为更多内容做好准备(以比以前更好的方式) 哇 - 这一切都有效,因为 JS 解码器可以看到每个命令现在是一个新行。
Best of luck!
祝你好运!
回答by Arslan Tabassum
Working example in php.
First Alert then Redirect works....
Enjoy...
php 中的工作示例。
首先警报然后重定向工作......
享受......
echo "<script>";
echo " alert('Import has successfully Done.');
window.location.href='".site_url('home')."';
</script>";
回答by tanmayee
<head>
<script>
function myFunction() {
var x;
var r = confirm("Do you want to clear data?");
if (r == true) {
x = "Your Data is Cleared";
window.location.href = "firstpage.php";
}
else {
x = "You pressed Cancel!";
}
document.getElementById("demo").innerHTML = x;
}
</script>
</head>
<body>
<button onclick="myFunction()">Retest</button>
<p id="demo"></p>
</body>
</html>
This will redirect to new php page.
这将重定向到新的 php 页面。