Andre Oliveira
Andre Oliveira

Reputation: 357

how to change a numeric value that is in jQuery by a variable coming from php

I need to replace the value 17 for a variable coming from php.php:

$(function() {
 $("#test").paginate({
  count    : 17,
  start    : 1,
  display  : 12,
  border   : true
  ...
 });
});

I tried this (didn't work):

$(function(){
$("#test").paginate({
 count   : $.post("php.php",function(result){ console.log(result['count']) }),
 start   : 1;
 display : 12;
 border  : true
 ...
});

php.php

$query = mysql_query("SELECT * FROM test");
$count = mysql_num_rows($query);
json_encode($count);

I am trying this way but i don't know if it's the best way to do that. I appreciate any suggest and help.

Upvotes: 0

Views: 94

Answers (1)

Petah
Petah

Reputation: 46050

jQuery Ajax function are asynchronous, or in other words, return instantly then call a callback when they complete. You need to set the count inside the callback like so:

$(function(){
    $.post("php.php",function(result){ 
        $("#test").pag({
            count : result
        });
    });
});

As per our comments, for multiple values you would need something like

$(function(){
    $.post("php.php",function(result){ 
        $("#test").pag({
            count : result.count,
            start : result.start,
            display : result.display
        });
    });
});

PHP:

$query = mysql_query("SELECT * FROM test");
$count = mysql_num_rows($query);
echo json_encode(array(
    'count' => $result,
    'start' => 7,
    'display' => 10,
));

Upvotes: 1

Related Questions