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

  • Home
  • SEARCH
  • 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 6043229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:52:36+00:00 2026-05-23T06:52:36+00:00

I’m using jQuery + PHP on my website and I want to do something

  • 0

I’m using jQuery + PHP on my website and I want to do something like this:
To simplify things I’ve got a page with 2 buttons () and according to which I click I want to start a script in background (php script) so I do a simple thing with jquery:

$("#button1").click(function(){
    $.post("script.php", {val:"but1"}, function(result){
        alert(result); // I want something with the result
    });
});

$("#button2").click(function(){
    $.post("script.php", {val:"but2"}, function(result){
        alert(result);
    });
});

and in the script.php I’ve got a simple if statement deciding what I should do.
In the php script I’m downloading a file and I want to create a progress bar of the file download. The php script would return me values (echo percentage; flush();) in some time interval.
And here comes the problem – when I echo those percentage values and flush it refreshes it “just in php” but the jquery waits until the script is finished anyway. Is there a way to get those values as they appear (after flush) or is there a completely other approach to this? I can’t think of anything else right now, maybe I shouldn’t be using jquery at all.
Thanks for the help.

  • 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-05-23T06:52:36+00:00Added an answer on May 23, 2026 at 6:52 am

    You can store the progress in a text file or DB while running the script and then have another file get the result via AJAX calls.

    JS/HTML

    <script>
    $(function() {
        var id;
        var timeoutLength = 1000;
    
        $("#button1").click(function() {
            $("#result").html("");
    
            // Create unique identifier
            id = (new Date().getTime()) + "-" + Math.floor(Math.random()*100);
            $.get("script.php", {id: id}, function(result){
                $("#result").html(result);
            });
    
            setTimeout(checkProgress, timeoutLength);
        });
    
        function checkProgress() {
            $.get("script.php", {progress: true, id: id}, function(data) {
                prog = parseInt(data);
                // Display progress bar
                if(prog < 100) {
                    $("#progress").css({ display: 'block', width: prog });
                    $("#progress").html(prog + "%");
    
                    setTimeout(checkProgress, timeoutLength);
                } else {
                    $("#progress").css('display', 'none');
                }
            });
        }
    })
    </script>
    <button id="button1">Click</button>
    <div id="progress" style="background: green"></div>
    <div id="result"></div>
    

    script.php

    <?php
    
    function getProgress($file) {
        return (file_exists($file)) ? file_get_contents($file) : 0;
    }
    
    function setProgress($file, $progress) {
        file_put_contents($file, $progress);
    }
    
    // Remove invalid characters
    $id = str_replace('/[^a-zA-Z0-9\-_]/', '', $_GET['id']);
    $file = "progress-" . $id . ".txt";
    
    if(isset($_GET['progress'])) {
        echo getProgress($file);
    } else {
        // Do something and update progress
        for($i = 10; $i <= 100; $i += 10) {
            // Do something
    
            setProgress($file, $i);
            sleep(1);
        }
        unlink($file);
        echo "Result: " . rand(1, 100);
    }
    

    Edit:
    Added support for multiple requests and simple progress bar.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
Does anyone know how can I replace this 2 symbol below from the string
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
In order to apply a triggered animation to all ToolTip s in my app,
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.