Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8486475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:57:19+00:00 2026-06-10T20:57:19+00:00

The context is this: The user click on a button The page is refreshed

  • 0

The context is this:

  1. The user click on a button
  2. The page is refreshed with new information on the page (echo is used to print html code).
  3. A file.txt is created
  4. The download of the created file should start automatically

With the code below, the file is created and the remaining part of the page is presented, but no download starts. Also, if I click on the link, the download doesn’t start, but the txt file is opened in the browser (whilst I want to force the download).

On the other hand, if I comment the javascript and uncomment the header instructions, I refresh the correct page and download the txt file, but the content of that file is wrong (it contains the html code echoed in the rest of the page).

=================NOT WORKING CODE==================

      echo "
      <form name=\"fn\" action=\"index.php?option=com_comp\" method=\"post\">
      // more not related stuff
      <input type=\"image\" src=\"".JURI::root().
      "components/com_comp/images/download_icon.png\" .
      "\" name=\"downloadaddresses\">DOWNLOAD_RESULTS
      // more not related stuff";


      if($_POST['downloadaddresses_x']!=0) {

            $myfilename = "tmp/results.txt";
            $fh = fopen($myfilename, 'w');

            $recipients = $_POST['recipients'];
            $semicolon_separated = implode(";", $recipients);

            fwrite($fh, $semicolon_separated);
            fclose($fh);


            /*header('Content-disposition: attachment; filename='.$myfilename);
            header("Content-type: application/octet-stream");*/

            echo "<a href=\"".$myfilename."\" id=\"downloadlink\">
            This download should start automatically!</a>";
            echo "<script type=\"text/javascript\">
                    newwindow=function{
            window.open('".$myfilename."','name','height=400,width=200');
                    if (window.focus) {newwindow.focus()}}
                </script>";
        }           

========================MORE CODE THAT DOES NOT WORK==================

    <script type="text/javascipt">  
    var el = document.getElementById('downloadlink');
    if (document.createEvent) {
        var event = document.createEvent(\"MouseEvents\");
        event.initEvent(\"click\", true, true);
        el.dispatchEvent(event);
    }
    else if (el.click) {
        el.click();
    }
    </script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:57:20+00:00Added an answer on June 10, 2026 at 8:57 pm

    Instead of this click simulation code:

    <script type="text/javascipt">  
    var el = document.getElementById('downloadlink');
    if (document.createEvent) {
        var event = document.createEvent(\"MouseEvents\");
        event.initEvent(\"click\", true, true);
        el.dispatchEvent(event);
    }
    else if (el.click) {
        el.click();
    }
    </script>
    

    Can you use this:

    <script type="text/javascript">
        location.href = document.getElementById('downloadlink').getAttribute('href');
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do this: using(var context = new SampleEntities()) { User user =
I'm having a page call mycontent.html. When the user visits the url of this
I have an AuthorizationServlet which is called when user click button in login page.
I have page main.html in it i have this code in the body tag:
Refer to my code below, when user click on en button, the content will
I'm designing an HTML page which has one button. The user clicks the button
I am using this for sending file to user header('Content-type: application/zip'); header('Content-Length: ' .
I'm using jQuery AJAX to implement a voting system. I'm using this code: $(.vote_up).click(function(){
i have default.aspx page and one user control. usercontrol is having following code for
Context: User is currently in the following page: http://myinternaldomain.com/page/ Issue: When user clicks on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.