0

I have a small chat application for internal usage, this is light version just for example. I need few things, sorry for asking full source code but I try understand and learn from this example (I need it for few things).

Please find my source code:

<?php
if(isset($_GET['opt']))
{
if($_GET['opt'] == 'write')
{
$entry = $_POST['chat_input'];
$data = fopen("dat.txt","a+");
fwrite($data,"$entry\n");
fclose($data);
}
}

// jQuery auto refresh start 
$dat = file_get_contents ("dat.txt");
if($dat == ''){echo 'You do not chat.';}else
{
$number_of_post = substr_count($dat, "\n");
$post = explode ("\n",$dat);
for($i=0 ; $i <= $number_of_post-1 ; $i++)
{
echo 'User x wrote: '.$post[$i].'<br>';
}
}
// jQuery auto refresh end

// jQuery form without refresh start 
echo '<form method="post" action="index.php?opt=write">
<p><textarea rows="5" name="chat_input" cols="46"></textarea></p>
<p><input type="submit" value="Submit" name="B1"></p>
</form>';

// jQuery form withour refresh end     

?>

I need two things: - first is auto refresh iframe, when user write new post code must show this - second is post my form without refresh, just execute action

Thank you very much for help, appreciate this.

user3300811
  • 105
  • 1
  • 1
  • 7

1 Answers1

0

To send post without refreshing use AJAX or method POST.

To refresh iframe do as asked here: How to refresh an IFrame using Javascript? You can refresh iframe every second

setTimeout(function(){/*yourcodehere*/ },1000); 
Community
  • 1
  • 1
Mr.TK
  • 1,743
  • 2
  • 17
  • 22
  • I was try with this but whole page was refreshed. function Reload () { var f = document.getElementById('iframe1'); f.src = f.src; – user3300811 Mar 17 '14 at 12:19
  • http://jsfiddle.net/XLf32/3/ I saw no page refreshing - only spinner telling user that there is a data transfer. – Mr.TK Mar 17 '14 at 18:42