hi,
i want to display output line by line using jquery.i tried many times but it not works..pls help me..

Html Code:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
$('#sub').click(function(){
$('#msg').html("<center><img src='(1).gif' alt='LOADING...' width='20' height='20'/>Loading........</center>");

 $.ajax({
               type: "POST",
               url: "ping1.php",

               data: $("#form1").serialize(), 

               success: function(data)  {
               $('#msg').html(data);
                alert(data);

                }
            });

       return false;
    });
 });
</script>
<div id="tag">


<form id="form1" method="" action="">
<input name="ip" type="text"/>
<!--<button id="sub">submit</button>-->
<input type="button" name="submit" id="sub"  value="submit"/>
</form>
</div>
<div id="msg"></div

Php:

<?php




$ip=$_POST["ip"];

header('Content-Encoding: none;');

set_time_limit(0);

$handle = popen("ping -c 4 $ip", "r");

if (ob_get_level() == 0) ob_start();
while(!feof($handle)) {

$buffer = fread($handle,1024);
$buffer = trim(htmlspecialchars($buffer));

echo $buffer . "<br />";
echo str_pad('', 4096); 

ob_flush();
flush();
}

pclose($handle);
ob_end_flush();





?>

if i submit the form using php it will show the output in line by line..but i use jquery to submit the form means it will display the output but not in line by line.

Dani AI

Generated

Short answer: your jQuery $.ajax success handler only runs when the full HTTP response is complete, so a single AJAX POST will usually deliver all ping output at once. is correct that polling a file (have the ping process append each line to a results file and poll it) is the simplest, most-compatible workaround. Below are safer and cleaner alternatives plus practical tips.

If you want true streaming (line-by-line) in modern browsers, either use Server‑Sent Events (SSE) or consume the XHR response progressively. SSE is simplest for server-to-client text streams; client example:

var es = new EventSource('stream.php?ip=1.2.3.4');
es.onmessage = function(e) { $('#msg').append(e.data + '<br>'); };

An XHR2 approach gives you more control (attach onprogress and read xhr.responseText as it grows). With jQuery you can supply an xhr factory and add onprogress there to append only the newly-received chunk.

Practical tips and cautions:

  • The server must send chunked/plain output and flush frequently; disable PHP and webserver output buffering and compression (for example turn off zlib.output_compression and server-side buffering like nginx fastcgi_buffering), otherwise the client will not see chunks.
  • Validate and sanitize ip (use filter_var(..., FILTER_VALIDATE_IP) and escapeshellarg() before passing to a shell) to avoid command injection.
  • If proxies/CDNs sit between client and server they may buffer chunked responses — in those environments polling the file (or using a background job + poll) is more reliable.
  • For very low-latency two-way interactions consider WebSockets; otherwise SSE or XHR streaming plus the above server tweaks give a robust per-line display.

Recommended Answers

All 5 Replies

First thing that comes to mind:

Make the PHP script that does the pinging (ping.php) write its results to, for example, ping_results.txt. Then, make an AJAX call to ping.php and immediatelty after execute a Javascript timeout that executes an AJAX function every 0.5 or 1 seconds, which opens ping_results.txt and outputs the results.

hi,
are you saying,i have to save ping.php results as a text file and then execute the result using ajax .

Yea. Because if you just call the PHP script that is pinging, it will wait untill all pinging is done and return everything at once. If you make each ping write a line to a text file for example, you could then check that text file very 0.5 seconds or so to check if any new lines have been added, and display those.

thank you..

hi,
i want permutation for all alphabets with word length is 8.but i get only permutation for 4.pls help me to debug this issue

code:

<?PHP



    FUNCTION permutations($letters,$num){ 


$last = STR_REPEAT($letters{0},$num); 
$result = ARRAY(); 
WHILE($last != STR_REPEAT(lastchar($letters),$num)){ 
echo "$last";
$result[] = $last; 
$last = char_add($letters,$last,$num-1); 
} 
$result[] = $last; 
RETURN $result; 
} 
FUNCTION char_add($digits,$string,$char){ 
IF($string{$char} <> lastchar($digits)){ 
$string{$char} = $digits{STRPOS($digits,$string{$char})+1}; 
RETURN $string; 
}ELSE{ 
$string = changeall($string,$digits{0},$char); 
RETURN char_add($digits,$string,$char-1); 
} 
} 
FUNCTION lastchar($string){ 
RETURN $string{STRLEN($string)-1}; 
} 
FUNCTION changeall($string,$char,$start = 0,$end = 0){ 
IF($end == 0) $end = STRLEN($string)-1; 
FOR($i=$start;$i<=$end;$i++){ 
$string{$i} = $char; 
} 
RETURN $string; 
} 
?> 








<?php
for($j=1;$j<9;$j++){
$Array=permutations("ABCDEFGHIJKLMNOPQRSTUVWXYZ",$j); 
FOR($i=0 ; $i < COUNT($Array) ; $i++) { 
ECHO "$i." . $Array[$i] . "<BR>"; 
} 
}
?> 

output:

i want a output like this
a
b
c
d
e
f
.
.
.
.
.
z
aa
ab
ac
ad
.
.
.
.
.
.
az
.
.
.
.
.
.
.
.
.
.
zzzzzzzz

But the above code shown like this
a
b
c
d
.
.
.
.
.
aa
ab
ac
.
.
.
.
az
.
.
.
zzzz
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.