PHP: issue popup warning and then redirect page

I am new to PHP.

When someone uploads a file size too large, I want to show them a pop-up warning and redirect them to the 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 won't show a warning popup.

+5
source share
4 answers

Do something like

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

Then on index.php ...

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

In other words, it index.phpalways checks to see if a message has been sent to it in the URL. If it is, display it. Then just pass the message to redirect

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

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

, , ,

+9

, header("location:index.php"); 302. , .

javascript , , .

+1
<script type="text/javascript">
alert("YOUR MESSAGE HERE");
location="REDIRECTION_PAGE.php";
</script>
+1

:

if($_FILES['file']['size'] > 200000) //any file size, 200 kb in this case
{
 echo "<script type='javascript'>alert('File size larger than 200 KB')</script>";
}
header("Location: index.php");

index.php, , . , , .

-1

All Articles