具有不同Id的多按钮事件的JQuery函数

时间:2015-08-26 06:58:40

标签: php jquery

以下代码创建了多个页面按钮。

<?php
    //Start session
    session_start();

    //Include database connection details
    require_once('connection.php');

    //Array to store validation errors
    $errmsg_arr = array();

    //Validation error flag
    $errflag = false;

    //Function to sanitize values received from the form. Prevents SQL injection
    function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysqli_real_escape_string($str);
    }

    //Sanitize the POST values
    $username = clean($_POST['username']);
    $password = clean($_POST['password']);

    //Input Validations
    if($username == '') {
        $errmsg_arr[] = 'Username missing';
        $errflag = true;
    }
    if($password == '') {
        $errmsg_arr[] = 'Password missing';
        $errflag = true;
    }

    //If there are input validations, redirect back to the login form
    if($errflag) {
        $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
        session_write_close();
        header("location: index.php");
        exit();
    }

    //Create query
    $qry="SELECT * FROM member WHERE username='$username' AND password='$password'";
    $result=mysqli_query($qry);

    //Check whether the query was successful or not
    if($result) {
        if(mysqli_num_rows($result) > 0) {
            //Login Successful
            session_regenerate_id();
            $member = mysqli_fetch_assoc($result);
            $_SESSION['SESS_MEMBER_ID'] = $member['mem_id'];
            $_SESSION['SESS_FIRST_NAME'] = $member['username'];
            $_SESSION['SESS_LAST_NAME'] = $member['password'];
            session_write_close();
            header("location: home.php");
            exit();
        }else {
            //Login failed
            $errmsg_arr[] = 'user name and password not found';
            $errflag = true;
            if($errflag) {
                $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
                session_write_close();
                header("location: index.php");
                exit();
            }
        }
    }else {
        die("Query failed");
    }
?> 

此处下方的页面视图显示为

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

在下面的代码中,我想只捕获那个由用户触发的值的id。我不知道id名称。

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

我的问题是,为什么我的脚本在点击任何页面后附加了行错误。当我点击2时,它会附加10行。之后我点击3它附加6行,然后我点击4它附加1行。为什么呢?

我的控制器页面位于

之下
<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>

2 个答案:

答案 0 :(得分:0)

另一种简单的方法是使用javascript onclick事件

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

将你的ajax代码放在JS中

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

答案 1 :(得分:0)

第15行错误。

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