0

I am using index.php

<script type="text/javascript">
    $(function(){
        $("#progressbar").progressbar({
            value: 0 
        });

        function load() {
            $.ajax({
                url: './ajax.php?status='+$( "#progressbar" ).progressbar( "value" ),
                success: function(data) {

                    ajax = eval('(' + data + ')');

                    if(ajax!=false) {
                        $("#progressbar").progressbar({
                            value: ajax.status
                        });

                        $("#message").html( ajax.message );

                        if(ajax.status!=100) {
                            load();
                        }
                    }
                }
            });
        }

        load();
    });
</script>

<div id="message"></div>

for a live ajax-based progress information. This works with ajax.php:

<?php

    $php_array['status'] = rand(0,99);

  if($php_array['status']>100) {
      $php_array['status'] = 100;
  }

  if($php_array['status'] != 100) {
      $php_array['message'] = 'Aktueller Status <b>'.$php_array['status'].'%</b> von 100%, Differenz: '.(100-$php_array['status']);
  } else {
      $php_array['message'] = 'Juhu endlich geschafft!';
  }


  // Ausgabe des PHP Arrays als JSON Objekt
  echo json_encode($php_array);
?>

which shows a random number between 0 and 99 in index.php, so it works.

I modified ajax.php to

<?php

  //modified - start
  $zahl = 0;
  $menge = 0;

  if ($handle = opendir('folder')) 
  { 
    while (false !== ($entry = readdir($handle))) 
    {
        if (strpos($entry,$_GET['session_id']) !== false) 
        {                                        
            if (strpos($entry,'result') !== false)
            {
            }
            else
            {
                $zahl += (int)file_get_contents('folder/'.$entry);
                $menge++;        
            } 
        }
    }
    closedir($handle);
  }

  if((int)($zahl/$menge) != 0.0)
  { 
    $php_array['status'] = (int)($zahl/$menge);
  }
  else
  {
    $php_array['status'] = 0;
  }

  //modified - end

  if($php_array['status']>100) {
      $php_array['status'] = 100;
  }

  if($php_array['status'] != 100) {
      $php_array['message'] = 'Aktueller Status <b>'.$php_array['status'].'%</b> von 100%, Differenz: '.(100-$php_array['status']);
  } else {
      $php_array['message'] = 'Juhu endlich geschafft! <img src="http://d4nza.de/blog/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley"> ';
  }
  echo json_encode($php_array);
?>

The opendir-part iterates through all output-text-files, which contain only a number between 0 and 100. Goal is to find the average progress. Then in

(int)($zahl/$menge)

it is being converted to an integer and forwarded via json to the index.php.

This works sometimes. That means, on index.php sometimes I see the progress, sometimes I don't.

How can I make the code in the index.php make it wait for the ajax.php to respond and not (presumably) abort prematurely?

  • please use `$.getJSON`. `eval()` is just asking for problems. – Ven Apr 21 '13 at 18:13
  • I read "Not all browsers have native JSON support so there will be times where you need to use eval() to the JSON string" at http://stackoverflow.com/questions/1843343/json-parse-vs-eval – Peter Weter Apr 21 '13 at 18:16
  • 1
    Use JSON2 or JSON3 then. Much faster than eval. – Ven Apr 21 '13 at 18:27
  • @PeterWeter: Not any more (you don't want to support such old browsers anyway). And you still could use `$.parseJSON` if you had to, but with the correct MIME type jQuery's ajax function does that for you already. – Bergi Apr 21 '13 at 18:48
  • @Bergi What does this code look like using your approach? – Peter Weter Apr 21 '13 at 20:43
  • @user1737909 What does this code look like using your approach? – Peter Weter Apr 21 '13 at 20:43
  • http://api.jquery.com/$.getJSON quite the same :) – Ven Apr 21 '13 at 20:50
  • Just `var ajax = data;`. And don't forget [correct HTTP header for json file](http://stackoverflow.com/q/267546/1048572) – Bergi Apr 22 '13 at 09:54

0 Answers0