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.

Answers


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,
));

Need Your Help

How to add NSViewController to a responder chain?

objective-c macos cocoa nsviewcontroller nsresponder

I'm having hard time understanding and finding info about how to make NSViewController accept key and mouse events. I read somewhere that in order to register these events in NSViewController it sh...

Beginner Android why is "id" is "Unknown member" with R.id?

android

I'm sure this is something dead simple as it's my first android app with code (the hello world example I did was just assigning a value to a string in XML). My problem is when trying to get the