2

This below code create multiple page button.

<?php
for($i=1; $i<=$this->total_pages; $i++)
{
  echo "<button class='btn page' id='next".$i."' value='".$i."'>".$i."</button>";
}
?>

here below the view of page is show like

1 2 3 4 5 6 ........//I dont know how many pages

Here in below code i want to catch only that id with value which is trigger by user. I don't know the id name.

<script>
    $( document ).ready(function() {
     $('button[id^="next"]').on('click', function() {
        var page = ($(this).attr('value'));
        $.ajax({
          type: "GET",
          url: 'index.php?act=product',
          data: ({page:page}),
          success: function(data) {
            var my_rows = $(data).find('tbody').html();
            $('tbody').append(my_rows);
          }
        });
        $(this).hide();
     });
    });
</script>

My question is that why my script is append rows wrong after click on any page. when i click on 2 it appends 10 rows. after then i click on 3 it appends 6 rows after then i click on 4 it appends 1 row. why?

My controller page is below

<?php
include "model/login_class.php";
include "view/template/product_class.php";
$tplLogin=new LoginTpl();
$sqlLogin=new sqlLogin();
//echo $_GET['page']; exit;
$total_results = $sqlLogin->totalproduct();
$per_page = 5;
$total_pages = ceil($total_results / $per_page);
$tplLogin->total_pages = $total_pages;
if (isset($_GET['page'])) {
    $show_page = $_GET['page']; //current page
    if ($show_page > 0 && $show_page <= $total_pages) {
        $start = ($show_page - 1) * $per_page;
        $end = $start + $per_page;
    } else {
        // error - show first set of results
        $start = 0;              
        $end = $per_page;
    }
} else {
    // if page isn't set, show first set of results
    $start = 0;
    $end = $per_page;
}
// display pagination
$sqlLogin->start = $start;
$sqlLogin->end = $end;
$tplLogin->products = $sqlLogin->product();
$tplLogin->product();
?>
6
  • so what is happening Commented Aug 26, 2015 at 7:00
  • 1
    The only problem I see is exit; - why is it there Commented Aug 26, 2015 at 7:01
  • $('button[id^="next"]').hide(); - should be $(this).hide() Commented Aug 26, 2015 at 7:02
  • remove brackets from data part data: ({page:page}), Commented Aug 26, 2015 at 7:03
  • Hi Arun, why my script append all rows even i did click on only page 2. Commented Aug 26, 2015 at 7:49

4 Answers 4

1

No need to know id, use separate class for button

Just try this.,

$('button.page').on('click', function() {
/*your class */
        var page = $(this).attr('value');
        $.ajax({
          type: "GET",
          url: 'index.php?act=product',
          data: {page:page},
          success: function(data) {
            var my_rows = $(data).find('tbody').html();
            $('tbody').append(my_rows);
          }
        });
        $("button.page").show();
        $(this).hide();
     });

I hope this will help you :)

Sign up to request clarification or add additional context in comments.

1 Comment

@Amit this is working on my side(: & you need to get id?
0

Another easy way to do this is to have a javascript onclick event

echo "<button class='btn page' id='next".$i."' value='".$i."' 
onclick='renderPage(".$i.")' >".$i."</button>";

put your ajax code in the JS like

<script>
   function renderPage(value){
   ...
   }
</script>

2 Comments

I downvoted it. You should attach events in your code, not in your HTML (i.e. using the onclick property).
may I know your priority behind this? @JustinJohnson
0

Another easy way to perform any operation for multiple buttons with different id is by using one common class name(".page") for all buttons and using that class name in JS for click event.

Here is the example HTML and JS code:

<button class='btn page' id='next1' value='1'>1</button>
<button class='btn page' id='next2' value='2'>2</button>
<button class='btn page' id='next3' value='3'>3</button>

  $(document).ready(function() {
     $('.page').on('click', function() {
       alert($(this).val());//Getting value for clicked button
       alert($(this).attr("id"));//Getting id for clicked button

       $.ajax({
         ...//Ajax call
       });
       $('this').hide();/Hiding clicked button
    });
  });

4 Comments

It is working on my side. With my code i am using 2.1.3/jquery.min.js version. Can you tell me which version of JQuery you are using?
Hi Amit, Can you tell me what exactly Output you want with your JS code ? I mean like what you are trying to do and what output is coming.
Hi Akshay, thanks for your help. I posted my answer. check it. I gave you reputation for your nice help and care. Thanks a lot.
hey Akshay after my 15 reputation it will be added in your account Stackoverflow says that.
0

line number 15 was wrong.

<?php
    include "model/login_class.php";
    include "view/template/product_class.php";
    $tplLogin=new LoginTpl();
    $sqlLogin=new sqlLogin();
    //echo $_GET['page']; exit;
    $total_results = $sqlLogin->totalproduct();
    $per_page = 5;
    $total_pages = ceil($total_results / $per_page);
    $tplLogin->total_pages = $total_pages;
    if (isset($_GET['page'])) {
        $show_page = $_GET['page']; //current page
        if ($show_page > 0 && $show_page <= $total_pages) {
            $start = ($show_page - 1) * $per_page;
            $end = $per_page;
        } else {
            // error - show first set of results
            $start = 0;              
            $end = $per_page;
        }
    } else {
        // if page isn't set, show first set of results
        $start = 0;
        $end = $per_page;
    }
    // display pagination
    $sqlLogin->start = $start;
    $sqlLogin->end = $end;
    $tplLogin->products = $sqlLogin->product();
    $tplLogin->product();
    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.