Skip to content
Advertisement

if php boolean is true run php script in background in html page

I have a html/php web page, if something runs as it should my boolean is set to true. If it is set to true i want to run a php script that downloads a file for the user. Currently i have it as a button that when pressed will download the file, my button looks like this:

<input type="button" value="Export CSV" onclick="window.open('bulk_out.php', '_blank')">

Is it possible to auto run the bulk_out.php if a boolean is set to true? I have tried to check the variable and put in a onload action, while the variable is true but no download happens.

<?php
if($worked == TRUE){
    ?>
<div onload="window.open('bulk_out.php', '_blank')">CSV downloaded to your machine. </div><?php }

I know $worked is set to true because the text “CSV downloaded to your machine.” shows on the page.

Advertisement

Answer

Why are not using the direct JavaScript approach? Try the following code, I’m pretty sure it will work. If download starts, then afterwards you could show confirmation message if you want. Let me know if it works:

if ($worked == true) {
    echo '<script>window.open("bulk_out.php", "_blank")</script>';
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement