+ Ajax更新页面无需重新加载

时间:2018-05-22 11:42:50

标签: javascript php jquery ajax refresh

当我登录系统时,它会显示我所拥有的书,当我点击链接书时,如果它们正在下降而不刷新页面,它将按升序对书进行排序,如果我再次点击它将按顺序对它们进行排序没有刷新页面,,,在我的代码中,当我点击链接时他没有做的事情,我打开检查元素他传递order_by_value:DESC当我再次点击时他传递参数order_by_value:ASC但他没有&# 39;为什么要采取行动?

此函数getBook in UUser.php

 public function getBooks($start = 0, $limit = 2, $order = "ASC")
  {
   $sql_start = $start * $limit;
   $sql_limit = $limit;
   $sql_order_by = $order;

   $query = "SELECT Library.nameOfBook, userBook.book_id, userBook.user_id FROM loginUser JOIN userBook JOIN Library ON userBook.user_id = loginUser.id AND userBook.book_id = Library.id WHERE loginUser.username=:username ORDER BY Library.nameOfBook $sql_order_by LIMIT $sql_start, $sql_limit";
   $statment = $this->db->prepare($query);
   $statment->execute([
       ':username' => $this->username
   ]);
   $result = $statment->fetchAll();

  echo "<table id='myTable' border='1'>

  <tr>
   <th><a id='sorter' href='#'>Books</a></th>
   <th>Action</th>
   </tr>";
   foreach($result as $row){
       echo "<tr>";
       echo "<td>" . $row['nameOfBook'] . "</td>";
       echo "<td>" ."<input type='submit' id='delete".$row['book_id']."-".$row['user_id']."' onclick='deleteBook(this)' name='delete' value='Delete'>" . "</td>";
       echo "</tr>";
   }

   echo "</table>";
   echo "";
   return count($result);
 }  

这是我在index.php中的ajax

 $(document).ready(function() {
 $("#sorter").click(function(e) {
 e.preventDefault();
 var order_by_value = $('input[name="order_by"]').val();

    $.ajax({
      type: 'GET',
       url: "" + 'UUser.php',
      data: {
     'order_by_value': order_by_value,
     // button: 'button',
      },
      success: function (res) {
        if(order_by_value == "ASC"){
       $('input[name="order_by"]').val("DESC");
      }
      else {
       $('input[name="order_by"]').val("ASC");
       }
   // $('input[name="current"]').trigger('click');
    }
   });
});
});

这个脚本在index.php中显示下一个并显示表

<?php
 $start = intval(isset($_POST['start'])?$_POST['start']:0);
 $order_by_hidden = isset($_POST['order_by'])?$_POST['order_by']:"ASC";
 if(isset($_POST['previous'])){
   $start = $start -1;
   //if($start < 1) $start = 1;
    $start= max($start, 1);
}
else if(isset($_POST['current'])){
    $start = $start;
}
else if(isset($_POST['next'])){
   $start = $start + 1;
}
else {
$start = 1;
}
?>

这也是我在index.php中的表格

 <form method="post" action="" id="form1">
 Name: <input type="text" name="user" value="<?php echo $user;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
password: <input type="password" name="pass" value="<?php echo $password;?>" >
<span class="error">* <?php echo $passwordErr;?></span>
<br><br>
<input type="submit" name="submit" value="Login">
<br><br>
<input type="hidden" value="<?php echo $start;?>" name="start"/>
<input type="hidden" value="<?php echo $order_by_hidden;?>" name="order_by"/>
 <button type="button" name="button" onclick="window.location.href='/ooRigester.php'">SignUp</button>
  <br><br>
  <input type='submit' name='previous' value='previous' <?php echo ($start <= 1 ? "disabled" : ""); ?>>
 <input type='submit' name='next' value='next'><br>
 <input type='button' name='current' value='current' style="display:none;"><br>
</form>

0 个答案:

没有答案