pagination in fbjs/ajax

Posted by fusion on Stack Overflow See other posts from Stack Overflow or by fusion
Published on 2010-05-09T18:47:03Z Indexed on 2010/05/09 23:38 UTC
Read the original article Hit count: 654

Filed under:
|
|
|
|

i've a search form in which i'm trying to implement pagination - getting the data through ajax. everything works out fine initially, except when i go to the next page or any of the links on the pagination. it gives me a page not found error. can anyone please point out what is wrong with my code?

search.html

<div class="search_wrapper">
  <input type="text" name="query" id="query" class="txt_search" onkeyup="submitPage('http://website/name/search.php', 'txtHint', '1');" />
 <input type="button" name="button" class="button_search" onclick="submitPage('http://website/name/search.php', 'txtHint', '1');" />
 <p>
 <div id="txtHint"></div>
</p>
</div>

search ajax.js:

function submitPage(url, target_id, page) {  
    // Retrieve element handles, and populate request parameters.  
    var target = document.getElementById(target_id);  

      if(typeof page == 'undefined') {
        page = 1; }


    // Set up an AJAX object.  Typically, an FBML response is desired.  
    document.getElementById(target_id).setInnerXHTML('<span id="caric"><center><img src="http://website/name/images/ajax-loader.gif" /></center></span>');
    var ajax = new Ajax();  
    ajax.responseType = Ajax.FBML;  
    ajax.requireLogin = true;  
    ajax.ondone = function(data) {  
        // When the FBML response is returned, populate the data into the target element. 
        document.getElementById('caric').setStyle('display','none');        
        if (target) target.setInnerFBML(data);  
    }  

    ajax.onerror = function() {
        var msgdialog = new Dialog();
        msgdialog.showMessage('Error', 'An error has occurred while trying to load.'); 
        return false;
    }


    var params = { 
        'query' : document.getElementById('query').getValue()

    };
    ajax.post(url, params, page);  
}

search.php:

$search_result = "";
if (isset($_POST["query"])) 
    $search_result = trim($_POST["query"]);

if(isset($_GET['page']))
    $page = $_GET['page'];
else
    $page = 1;


.....

            $self = $_SERVER['PHP_SELF'];
            $limit = 2; //Number of results per page
            $numpages=ceil($totalrows/$limit);

            $query = $query." ORDER BY idQuotes LIMIT " . ($page-1)*$limit . ",$limit";
            $result = mysql_query($query, $conn)
             or die('Error:' .mysql_error());

?>

<div class="search_caption">Search Results</div>
<div class="search_div">
<table>
. . .display results
</table>
</div>

<hr>
<div class="searchmain">
<?php
   //Create and print the Navigation bar
       $nav="";
       $next = $page+1;
       $prev = $page-1;
       if($page > 1) {
            $nav .= "<a onclick=\"submitPage('','','$prev'); return false;\" href=\"$self?page=" . $prev . "&q=" .urlencode($search_result) . "\">< Prev</a>";

            $first = "<a onclick=\"submitPage('','','1'); return false;\" href=\"$self?page=1&q=" .urlencode($search_result) . "\"> << </a>" ;
        }

        else {
            $nav .= "&nbsp;";
            $first = "&nbsp;";
        }

       for($i = 1 ; $i <= $numpages ; $i++) {
            if($i == $page) {
                $nav .= "<span class=\"no_link\">$i</span>";
            }else{
                $nav .= "<a onclick=\"submitPage('','',$i); return false;\" href=\"$self?page=" . $i . "&q=" .urlencode($search_result) . "\">$i</a>";
            }
        }

        if($page < $numpages) {
            $nav .= "<a onclick=\"submitPage('','','$next'); return false;\" href=\"$self?page=" . $next . "&q=" .urlencode($search_result) . "\">Next ></a>";

            $last = "<a onclick=\"submitPage('','','$numpages'); return false;\" href=\"$self?page=$numpages&q=" .urlencode($search_result) . "\"> >> </a>";
        }

        else {

             $nav .= "&nbsp;";
             $last = "&nbsp;";
        }

        echo  $first . $nav . $last;
?>

</div>

this is the link which displays on the next page:

http://apps.facebook.com/website-folder/search.php?page=2&q=good&_fb_fromhash=[some obscure number]

© Stack Overflow or respective owner

Related posts about facebook

Related posts about JavaScript