netpumber Posted September 4, 2012 Share Posted September 4, 2012 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 4, 2012 Share Posted September 4, 2012 Move it outside of the loop. Quote Link to comment Share on other sites More sharing options...
netpumber Posted September 4, 2012 Author Share Posted September 4, 2012 This doesn't work. Quote Link to comment Share on other sites More sharing options...
netpumber Posted September 4, 2012 Author Share Posted September 4, 2012 i tried also with flush() method but doesn't seem to work. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 5, 2012 Share Posted September 5, 2012 What, exactly, are you expecting to see? Quote Link to comment Share on other sites More sharing options...
trq Posted September 5, 2012 Share Posted September 5, 2012 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. Quote Link to comment Share on other sites More sharing options...
netpumber Posted September 5, 2012 Author Share Posted September 5, 2012 To achieve the effect your looking for, it would be best to use Ajax. Can you give me a hint about the ajax way ? Thank you. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 5, 2012 Share Posted September 5, 2012 Don't really need AJAX for this either, unless you absolutely need to retrieve the content from the server. A bog standard JavaScript that manipulates the content (innerHTML) of the textarea would be enough. Quote Link to comment Share on other sites More sharing options...
netpumber Posted September 5, 2012 Author Share Posted September 5, 2012 Can you give an example? Quote Link to comment Share on other sites More sharing options...
Adam Posted September 5, 2012 Share Posted September 5, 2012 What, exactly, are you expecting to see? People are just guessing what you want, netpumber. What exactly do you expect to happen? Quote Link to comment Share on other sites More sharing options...
netpumber Posted September 5, 2012 Author Share Posted September 5, 2012 "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 ? Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 5, 2012 Share Posted September 5, 2012 You need to use javascript Quote Link to comment Share on other sites More sharing options...
netpumber Posted September 5, 2012 Author Share Posted September 5, 2012 Have you got in mind a sample code to see ? Quote Link to comment Share on other sites More sharing options...
Barand Posted September 5, 2012 Share Posted September 5, 2012 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> Quote Link to comment Share on other sites More sharing options...
netpumber Posted September 6, 2012 Author Share Posted September 6, 2012 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 6, 2012 Share Posted September 6, 2012 keep this in mind: - PHP runs on the server - Javascript runs on the client Quote Link to comment Share on other sites More sharing options...
trq Posted September 6, 2012 Share Posted September 6, 2012 As I said, you need to look into Ajax. Quote Link to comment Share on other sites More sharing options...
netpumber Posted September 13, 2012 Author Share Posted September 13, 2012 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 14, 2012 Share Posted September 14, 2012 You would need to make an ajax request each time, the PHP page would return new information each time. Quote Link to comment Share on other sites More sharing options...
netpumber Posted September 14, 2012 Author Share Posted September 14, 2012 No you can not do this because php will not return anything if the vars array didn't get fulled. If the loop didn't stop you can not receive anything from the array. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.