0

I have a PHP script which downloads email attachments on a daily basis from cron. I would like to create a HTML form which can be used to run this script and others manually when necessary.

As the script runs there is a loop which displays the number of emails that have been run through and then at the end echos that the script has finished running.

I would like to be able to make it so that once the button is pressed on the form, a window will display these output's so that the user knows once the script has finished running.

The simple form I have so far:

<form action="test.php" type="post">
    <input type="submit" value="Download Attachments" />
</form>

Is it possible to achieve this? Right now the best way I can think of doing it is to redirect back to the form page after the script has finished running, but I would prefer it if everything could be on the same page so the user has a record of what scripts they have run, right in front of them.

3 Answers 3

5

It's so simple to achieve that. Refer the following sample code:

<?php 
    if(isset($_POST['submit'])){ 
         //code to be executed
    }else{
         //code to be executed  
    }
?>
<html>
<head></head>
<body>
     <form method="post" action="">
        <input type="submit" name="submit">
     </form>
</body>
</html>

The PHP Script will only run if the submit button is clicked.

Sign up to request clarification or add additional context in comments.

Comments

1

If you're using jQuery then you can use jQuery's Ajax, it is a simple function that returns the result of page.

This JavaScript will run as soon as the page loads:

$(document).ready(function() {
    //tell the user that the script is still running

    $.ajax({
        type: "POST",
        url: "getAttachments.php",
        success: function(data) {
            //data will contain the output from the page
        },
        complete: function() {
            //tell the user that the script has finished running
    });
});

You don't need jQuery to use ajax, but it makes it much easier and neater. You can see the version without jQuery here: https://www.w3schools.com/xml/ajax_intro.asp

Comments

0

yes you can with a test like :

if (!empty($_POST)) {
   // code to display your emails
}else{
   //code to display the from
} 

so you will be always on the same page and it's PHP who decide what should be displayed to the user, no need for a redirect or whatsoever

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.