2012-05-02 38 views
0

來的時候需要更換價值17從php.php未來變量:如何改變一個數值,是在jQuery的一個變量從PHP

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

我想這(沒有工作):

$(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); 

我想這樣,但我不知道它是否做到這一點的最好辦法。我感謝任何建議和幫助。

+1

什麼事件'.pag()'? – Blake

+0

@Blake。像'.gnm()' – gdoron

+0

@gdoron一樣你能把我鏈接到jquery文檔嗎?我根本找不到那個。 – Blake

回答

1

jQuery Ajax函數是異步的,或換句話說,立即返回,然後在完成時調用回調函數。您需要設置計數回調裏面,像這樣:

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

根據我們的意見,爲多個值,你會需要像

$(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, 
)); 
+0

的一個名稱,我打算將這個結果打印在頁面中,不是嗎?但我需要的是,而不是數字表示它需要查詢結果的值,並在那裏放置像:'計數:$計數' – Andre

+0

@Andre如果您的PHP頁面返回一個數字,那麼它應該正常工作。 – Petah

+0

我試過了,並沒有工作。我不知道我是否表達了自己的錯誤,但不承認它是一個數字。 – Andre

相關問題