Jump to content

Recommended Posts

Hello.

 

Here is a php code that executed if you press a submit button from a form.

 

 

 <textarea class="textarea"><?php
        if(isset($_POST['submit'])){
             $i =1;
              for($i=1;$i<=4;$i++){
               echo "hello\n";         
              }
        }
        ?>
</textarea>

 

How can i make it , to print out the "hello" word each time the loop runs and not all of them at the end.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/268004-eco-one-after-another/
Share on other sites

Look at your code. How exactly do you expect multiple "hello"'s to be printed in sequence within a textarea, when the text area itself isn't complete until after the "hello"'s are sent.

 

Server side languages process data server side and then send the results in one response.

 

To achieve the effect your looking for, it would be best to use Ajax.

"How can i make it , to print out the "hello" word each time the loop runs and not all of them at the end."

 

I mean to see something like this :

 

event 1 : submit button pressed

event 2 : "hello" (printed on textarea)

event 3 : "hello" (printed on textarea after 2-3 sec)

event 4 : "hello" (printed on textarea after 2-3 sec)

 

and so on.. Is that clear now ?

try this

 

<textarea id='txt' cols="30" rows="5"></textarea>
<br />
<button id="start" >Start</button>

<script type="text/javascript">

    document.getElementById("start").onclick = function() {
        count=4;
        document.getElementById("txt").value="";
        textloop();
    }

    var count = 4;

    function textloop()
    {
        var t = document.getElementById("txt");
        if (count>0) {
            t.value = t.value + "Hello\n";
            count--;
            setTimeout("textloop()", 2000 );
        }
        
    }
</script>

Thank you for the code.

 

Tried to change it and make it to take the value from a php loop and print it out in the textarea but nothing happened. With some way i have to synchronize php with javascript and i think this is impossible. Here is the code

 

<textarea id='txt' cols="30" rows="5"></textarea>
<br />
<script type="text/javascript">
        document.getElementById("txt").value="";
</script>

<?php

$i=0;
for($i=0;$i<=10;$i++)
{ ?>
<script type="text/javascript">
        var t = document.getElementById("txt");       
        t.value = t.value + <?php echo json_encode($i);?>;
</script>

<?php
}
?>

I also tried to use sleep(); but i had the same results.

 

Hello again.

 

I tried it out with ajax.

 

<html>
<head>

<script language="JavaScript" src="json.js"></script>
</head>
<body>

  <textarea id="text"></textarea>

<script type="text/javascript">
  
  var httpRequest; 
  var textarea = document.getElementById('text');

  
    if (window.XMLHttpRequest) { 
      httpRequest = new XMLHttpRequest();
    } 
     
    if (!httpRequest) {
      alert('Giving up  Cannot create an XMLHTTP instance');
      return false;
    }
    httpRequest.onreadystatechange = loopData;
    httpRequest.open('GET', 'http://localhost/json/data.php', true);
    httpRequest.send();
  
  function loopData()
  {
    setTimeout(alertContents,2000);
  }

  function alertContents() {
    if (httpRequest.readyState === 4) {
      if (httpRequest.status === 200) {
        
        var vars = JSON.parse(httpRequest.responseText);
        textarea.value = vars[0];
      } else {
        alert('There was a problem with the request.');
      }
    }
  }

  

</script>
</body>
</html>

 

At data.php file i create an array() called $vars that fills with a loop. Then ajax receives the output of data.php and print them in textarea.

 

The problem remains. Ajax has to wait till data.php end up with the loop , and then it (ajax) can receive the output.

 

I wanna make it work like this:

 

 

php file

ajax

in first loop $vars[0]="h"

print out h

in second loop $vars[1]="e"

print out e

in third loop $vars[2]="l"

print out l

 

and so on...

Any more hint ?

 

Thank you.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.