Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display alert message and redirect after click on accept

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.

like image 576
fedejp Avatar asked Aug 08 '12 17:08

fedejp


People also ask

How do I get alert messages after redirecting a page?

You cannot run an alert after the location. href has changed because it causes the browser to refresh. Once refreshed, your script is no longer running. You would need to move your alert script into your search page and perhaps pass the name as a querystring arguement.

How do I show alert before response redirect?

alert('Your Message');window. location='yourpage. aspx';</script>"); It will display pop up window message, then after redirect to another web page.

How redirect URL in PHP?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.

How show success message from another page in PHP?

The best way to solve this problem is set a session message after the success of your operation in the process page. Then in the redirected page check whether the session message is set or not. If it is set then simply echo that message.


4 Answers

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.

like image 182
Prasanth Avatar answered Oct 12 '22 13:10

Prasanth


use this code to redirect the page

echo "<script>alert('There are no fields to generate a report');document.location='admin/ahm/panel'</script>";
like image 24
Yogesh Prajapati Avatar answered Oct 12 '22 11:10

Yogesh Prajapati


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.

like image 3
M. Ahmad Zafar Avatar answered Oct 12 '22 11:10

M. Ahmad Zafar


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.

like image 1
Repox Avatar answered Oct 12 '22 13:10

Repox