Search code examples
phpajaxpaginationlivesearch

PHP Ajax live search in pagination


I am trying to implement live search in pagination. I am able to insert static search button that works after hit enter. I want to implement live search button instead this. When I try to do using ajax, I cannot handle, it is messed up.

Following is my pagination and search code. How to use live search instead of following static search? I am new in ajax. Or please provide a tutorial link to create live search in pagination.

<?php   
    $search_keyword = '';
    if(!empty($_POST['search']['keyword'])) {
        $search_keyword = $_POST['search']['keyword'];
    }
    $sql = 'SELECT * FROM posts WHERE post_title LIKE :keyword OR description LIKE :keyword OR post_at LIKE :keyword ORDER BY id DESC ';

    /* Pagination Code starts */
    $per_page_html = '';
    $page = 1;
    $start=0;
    if(!empty($_POST["page"])) {
        $page = $_POST["page"];
        $start=($page-1) * ROW_PER_PAGE;
    }
    $limit=" limit " . $start . "," . ROW_PER_PAGE;
    $pagination_statement = $pdo_conn->prepare($sql);
    $pagination_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
    $pagination_statement->execute();

    $row_count = $pagination_statement->rowCount();
    if(!empty($row_count)){
        $per_page_html .= "<div style='text-align:center;margin:20px 0px;'>";
        $page_count=ceil($row_count/ROW_PER_PAGE);
        if($page_count>1) {
            for($i=1;$i<=$page_count;$i++){
                if($i==$page){
                    $per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page current" />';
                } else {
                    $per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page" />';
                }
            }
        }
        $per_page_html .= "</div>";
    }

    $query = $sql.$limit;
    $pdo_statement = $pdo_conn->prepare($query);
    $pdo_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
    $pdo_statement->execute();
    $result = $pdo_statement->fetchAll();
?>
<form name='frmSearch' action='' method='post'>
<div style='text-align:right;margin:20px 0px;'><input type='text' name='search[keyword]' value="<?php echo $search_keyword; ?>" id='keyword' maxlength='25'></div>
<table class='tbl-qa'>
  <thead>
    <tr>
      <th class='table-header' width='20%'>Title</th>
      <th class='table-header' width='40%'>Description</th>
      <th class='table-header' width='20%'>Date</th>
    </tr>
  </thead>
  <tbody id='table-body'>
    <?php
    if(!empty($result)) { 
        foreach($result as $row) {
    ?>
      <tr class='table-row'>
        <td><?php echo $row['post_title']; ?></td>
        <td><?php echo $row['description']; ?></td>
        <td><?php echo $row['post_at']; ?></td>
      </tr>
    <?php
        }
    }
    ?>
  </tbody>
</table>
<?php echo $per_page_html; ?>
</form>

Solution

  • This is the first thing I got in my Google search result after searching for live search pagination:

    StackOverflow Link - pagination with live search .


    To Note: This method uses jQuery just so you are aware.

    As Akshay pointed out, all you need is a search-box:

    <input type='text' Id="search_box" name='search' placeholder='search' />

    and your JS code as follows:

    $(document).ready(function() {
      change_page('0');
    });
    
    function change_page(page_id) {
      //To get the field value
      var search_val = $("#search_box").val();
      $(".flash").show();
      $(".flash").fadeIn(400).html('Loading <img src="ajax-loader.gif" />');
      var dataString = 'page_id=' + page_id + '&search=' + search_val;
      $.ajax({
        type: "POST",
        url: "paging.php",
        data: dataString,
        cache: false,
        success: function(result) {
          $(".flash").hide();
          $("#page_data").html(result);
        }
      });
    }
    

    All you now need to do is to seperate your PHP code into a separate .php file and reference the data from there in $.ajax url parameter.