[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 504
  • Last Modified:

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?
0
mgtm3
Asked:
mgtm3
  • 5
  • 5
  • 2
  • +1
3 Solutions
 
drowning_in_the_howCommented:
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
 
BadotzCommented:
>>#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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
striker46Commented:
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now