how can i make ajax call a php file every one second using javascript?

i have a script that gets the updates from a database and i thought i would be cool if i make it ajax so that the script updates every one second with out clicking on refresh

can i have a script?
mgtm3Asked:
Who is Participating?
 
striker46Connect With a Mentor Commented:
I wrote a tutorial on how to refresh the content of a DIV in Ajax automatically, without having to use frameworks.

http://www.aleixcortadellas.com/main/?p=232

In the PHP file which is called, just write the MySQL query and this should be it.

Hope this helps.
0
 
drowning_in_the_howConnect With a Mentor Commented:
Be careful making an AJAX call every second. There are a few problems with this.
#1. AJAX calls can be memory and processor intensive on the browser and the more often you do them, the more the browser will lag while the page is open.
#2. It usually takes more than 1 second to send a request and to get the response, especially if you are on a connection less than broadband. Even on an internal network, it can still take more than a second to make an AJAX call and to get the response.

So in short, try spacing the requests out, more like 5 seconds.

Are you using any kind of JavaScript framework?
Do you want the return data to come back as JSON, XML or an HTML chunk?
0
 
BadotzConnect With a Mentor Commented:
>>#2. It usually takes more than 1 second to send a request and to get the response

In my experience, it takes far less time than a second. YMMV.

Polling the server every second sounds ridiculous; what in the world are you trying to do?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
BadotzCommented:
Oh, right:

"i thought i would be cool if i make it ajax so that the script updates every one second" (sic)

What is "cool" about dragging your page to its knees?
0
 
mgtm3Author Commented:
thank you striker46 it works great just one problem the things that comeback from the boo.php is in arabic and when i get them back in ajax i get them not in arabic but in ý ýýý   how can i fix this
0
 
striker46Commented:
Let's see if I understand it well: the data retrieved from the MySQL database is stored there in arabic, but it is appears in the page as ýýýýý? Could be related with encoding...
0
 
mgtm3Author Commented:
noo when i get the data not using ajax i get it in arabic but now in ajax i get it all wrong
0
 
striker46Commented:
Can you please post the code you use to retrieve the data without AJAX?
I'll do some tests meanwhile
0
 
mgtm3Author Commented:
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />

<?php
include 'sql.php';


$select ="SELECT * FROM posts order by id desc limit 50";
  $re = mysqli_query($mysqli,$select);

?><table width="100%"><?php
  while ($row=mysqli_fetch_array($re)){
  ?>
<tr><td><table align="right" ><tr align="right"><td><div align="right"><a href="get.php?id=<?php echo $row['id'];?>">more>></a></div></td><td  ><div align="right">
  <?php
echo $row['topic']."   ".$row['posts']." *9DJB "."   ".$row['forumid'];
?>
</div>
  <font style="font-weight:900"></td><td><div align="right"><?php echo $row['id'];?></div>
  </td>
</tr></table></td></tr>
</font><?php } ?>
</table>
0
 
mgtm3Author Commented:

<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
 
<?php
include 'sql.php';
 
 
$select ="SELECT * FROM posts order by id desc limit 50";
  $re = mysqli_query($mysqli,$select);
 
?><table width="100%"><?php
  while ($row=mysqli_fetch_array($re)){
  ?>
<tr><td><table align="right" ><tr align="right"><td><div align="right"><a href="get.php?id=<?php echo $row['id'];?>">more>></a></div></td><td  ><div align="right">
  <?php
echo $row['topic']."   ".$row['posts']." *9DJB "."   ".$row['forumid'];
?>
</div>
  <font style="font-weight:900"></td><td><div align="right"><?php echo $row['id'];?></div>
  </td>
</tr></table></td></tr>
</font><?php } ?>
</table>

Open in new window

0
 
striker46Commented:
Try adding to the boo.php file the following line:

<?php header('Content-Type: text/html; charset=windows-1256');?>

Let's see if it helps.
0
 
mgtm3Author Commented:
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh striker46 you are my hero thanksssssssssssssssssss
0
 
striker46Commented:
Glad to know you got it to work!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.