We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

mgtm3
mgtm3 asked
on
Medium Priority
549 Views
Last Modified: 2012-08-14
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?
Comment
Watch Question

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?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2007
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?
Top Expert 2007

Commented:
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?
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.

Author

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
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...

Author

Commented:
noo when i get the data not using ajax i get it in arabic but now in ajax i get it all wrong
Can you please post the code you use to retrieve the data without AJAX?
I'll do some tests meanwhile

Author

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>

Author

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

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.

Author

Commented:
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh striker46 you are my hero thanksssssssssssssssssss
Glad to know you got it to work!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.