Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: give alert popup then redirect the page

Tags:

php

echo

header

I am new to PHP.

When someone uploads a file size too big, I want to show them a warning popup and redirect them to a previous page (or vice versa).

if(file size is too big){    
   ob_start();   
   header("location:index.php");    
   echo "<script type='text/javascript'>alert('Your File Size is too big!');</script>";   
   ob_end_flush();   
   exit;    
}

This code above will just redirect me to index.php and doesn't show any warning popup.

like image 663
Eric Kim Avatar asked Jul 28 '12 19:07

Eric Kim


2 Answers

<script type="text/javascript">
alert("YOUR MESSAGE HERE");
location="REDIRECTION_PAGE.php";
</script>
like image 41
Marco Avatar answered Sep 20 '22 13:09

Marco


Do something like

header("Location: index.php?Message=" . urlencode($Message));

Then on index.php...

if (isset($_GET['Message'])) {
    print $_GET['Message'];
}

In other words, index.php will always check if it's being passed a message in the url. If there is one, display it. Then, just pass the message in the redirect

if you really want to use a modal popup, generate the js...

if (isset($_GET['Message'])) {
    print '<script type="text/javascript">alert("' . $_GET['Message'] . '");</script>';
}

Note that this will break if you use quotes in the message unless you escape them

like image 140
Basic Avatar answered Sep 18 '22 13:09

Basic