php 单击接受后显示警报消息和重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11869662/
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
Display alert message and redirect after click on accept
提问by fedejp
Well, I have a page with links to reports. Whenever somebody clicks on one report, they can download the excel file. However, sometimes there are no fields to make a report; in that case, I want to display an alert message and after they click on "accept", they get redirected to the main panel. When they click on the report, they go to a controller that uses a switchto get the data. If there's no data, the model returns FALSE; so at the end of the controller, I check:
好吧,我有一个包含报告链接的页面。每当有人点击一份报告时,他们就可以下载 Excel 文件。但是,有时没有字段可以进行报告;在这种情况下,我想显示一条警报消息,在他们单击“接受”后,他们将被重定向到主面板。当他们点击报告时,他们会转到使用 aswitch来获取数据的控制器。如果没有数据,模型返回FALSE;所以在控制器的末尾,我检查:
if ($result_array != FALSE)
to_excel($result_array->result_array(), $xls,$campos);
else {
echo "<script>alert('There are no fields to generate a report');</script>";
redirect('admin/ahm/panel');
}
If I get rid of redirect('admin/ahm/panel');then the alert works, but it moves the user to the page that was supposed to generate the excel file. But if I use the redirect, the controller moves the user to the main panel without showing the alert.
如果我摆脱redirect('admin/ahm/panel');了警报,那么警报会起作用,但它会将用户移动到应该生成 excel 文件的页面。但是如果我使用重定向,控制器会将用户移动到主面板而不显示警报。
Any help is appreciated.
任何帮助表示赞赏。
Thanks in advance.
提前致谢。
回答by Prasanth
echo "<script>
alert('There are no fields to generate a report');
window.location.href='admin/ahm/panel';
</script>";
and get rid of redirectline below.
并去掉redirect下面的线。
You were mixing up two different worlds.
你把两个不同的世界混在一起了。
回答by Yogesh Prajapati
use this code to redirect the page
使用此代码重定向页面
echo "<script>alert('There are no fields to generate a report');document.location='admin/ahm/panel'</script>";
回答by M. Ahmad Zafar
Combining CodeIgniter and JavaScript:
结合 CodeIgniter 和 JavaScript:
//for using the base_url() function
$this->load->helper('url');
echo "<script type='javascript/text'>";
echo "alert('There are no fields to generate a report');"
echo "window.location.href = '" . base_url() . "admin/ahm/panel';"
echo "</script>";
Note:The redirect()function automatically includes the base_urlpath that is why it wasn't required there.
注意:该redirect()函数会自动包含base_url路径,这就是为什么不需要它的原因。
回答by iykasu
echo "<script>
window.location.href='admin/ahm/panel';
alert('There are no fields to generate a report');
</script>";
Try out this way it works...
试试这种方式它的工作...
First assign the window with the new page where the alert box must be displayed then show the alert box.
首先为必须显示警报框的新页面分配窗口,然后显示警报框。
回答by Repox
The redirectfunction cleans the output buffer and does a header('Location:...');redirection and exits script execution. The part you are trying to echo will never be outputted.
该redirect函数清除输出缓冲区并进行header('Location:...');重定向并退出脚本执行。您尝试回显的部分将永远不会输出。
You should either notify on the download page or notify on the page you redirect to about the missing data.
您应该在下载页面上通知或在您重定向到的页面上通知丢失的数据。
回答by Eduardo Luciano Ramos
This way it works`
这样就可以了`
if ($result_array)
to_excel($result_array->result_array(), $xls,$campos);
else {
echo "<script>alert('There are no fields to generate a report');</script>";
echo "<script>redirect('admin/ahm/panel'); </script>";
}`
回答by Dexter
that worked but try it this way.
那行得通,但试试这种方式。
echo "<script>
alert('There are no fields to generate a report');
window.location.href='admin/ahm/panel';
</script>";
alert on top then location next
警报在顶部然后位置下一个

