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

Mocking AngularJS XHR requests for testing using Jasmine

angularjs xmlhttprequest jasmine

Ok, I am pretty sure this is really weird behaviour - and I have no idea what is going on...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.