Hi i have to set a message box with time out when a specific data is already available in the database table.and then redirect to another page.My issue is that when page is getting redirected without showing the alert box.
Control statements php:
$count=0;
$sql="SELECT main_sec_name from main_sec_temp";
if($r=mysqli_query($con,$sql)) {
    while($result= $r->fetch_row()) {
        $count=$count+1;        
    }
}   
if($count==0) { 
    if(mysqli_query($con,"INSERT INTO main_sec_temp(main_section_order,main_sec_name,main_number_of_ques,attempts) VALUES('','$sec_name','$no_question','$no_attempts')")) { 
        echo "Successfully Inserted";          
        header("location:index.php");       
    }else{        
        echo "Insertion Failed";        
    }
} else { //When Data is already exist 
    echo "<script>alert('already exist')</script>";
    header("location:index.php");   
}
 
     
    