简体   繁体   中英

Update a variable without refresh in PHP

I need to update a variable named $count (in the below code) automatically. When my database update, $count be update immediately because i need to use value of $count in android app so i can't refresh count.php file every time.

<?php
$con=mysql_connect("","","");
mysql_select_db("u607509006_bd1",$con);

$query = "select * from content where status='a'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
echo $count;
mysql_close($con);
?>

Just use a Jquery Ajax Request on the client side like so:

$.ajax({
  url: "path/to/code/file.php" // change this to your path to your code
}).done(function(data) {
  // This is executed after the ajax request is complete
  // data should be the updated value. You can also return other data // types e.g. JOSN
  $('#counter').text(data);
});

JS Fiddle Example: https://jsfiddle.net/anik786/nvkdLhv2/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM