Display alert message and redirect after click on accept
Asked Answered
M

8

17

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 switch to get the data. If there's no data, the model returns FALSE; so at the end of the controller, I check:

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.

Metternich answered 8/8, 2012 at 17:9 Comment(1)
Redirect first, notifying the page you redirect to that an alert needs to be shown.Middleweight
A
66
echo "<script>
alert('There are no fields to generate a report');
window.location.href='admin/ahm/panel';
</script>";

and get rid of redirect line below.

You were mixing up two different worlds.

Atc answered 8/8, 2012 at 17:17 Comment(4)
There's a problem with this. It just add the url that I want to go to to the current url. But it doesn't redirect. I even tried using the whole url (with http://) and still it doesn't redirect; just adds it to the current url and I get stuck on an endless alert loop.Metternich
edited. changed += to =. Earlier I specifically added to current url because I thought you wanted it. But anyways, should work now.Atc
This is not safe, disabling javascript would let further processing continue.Recapitulation
what is "window.location.href='admin/ahm/panel';"?Ewen
K
7

use this code to redirect the page

echo "<script>alert('There are no fields to generate a report');document.location='admin/ahm/panel'</script>";
Knur answered 21/5, 2016 at 4:20 Comment(0)
J
3

Combining CodeIgniter and 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_url path that is why it wasn't required there.

Jordison answered 8/8, 2012 at 17:57 Comment(0)
H
1

The redirect function 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.

You should either notify on the download page or notify on the page you redirect to about the missing data.

Habitue answered 8/8, 2012 at 17:14 Comment(0)
P
1
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.

Pharmacopsychosis answered 21/2, 2014 at 9:6 Comment(0)
H
0

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

Hemingway answered 19/9, 2014 at 14:47 Comment(0)
C
0

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>";
}`
Carman answered 24/2, 2017 at 11:38 Comment(0)
U
0

//functions.php:

    <?php
function fx_alert_and_redirect($msg, $page){
    **echo "<!DOCTYPE html><html><head>Login...</head><body><script type='text/javascript'>alert(\"" .$msg . "\");window.location.href=\"$page\";</script></body></html>";**
    

}
?>

//process_login-form.php:

<?php require_once '../app/utils/functions.php'; ?>

<?php

// ...

fx_alert_and_redirect("Mauvais nom d'usager ou mot de passe!", "../index.php?page=welcome");

?>
Untimely answered 28/1, 2023 at 5:53 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Powell

© 2022 - 2024 — McMap. All rights reserved.