PHP获取ajax table内容写法

Published
June 21, 2022
浏览次数 :  7

//call method
getExistingData(0,10); //获取从第一条开始的前10条数据
//ajax
function getExistingData(start,limit) {
    $.ajax({
      url:'ajax.php',
      method:'POST',
      dataType:'text',
      data:{
        key:'getExistingData',
        start:start,
        limit:limit
      },
      success:(response) => {
        if (response != 'reachedMax') {
          //这里直接append response 的话 直接从ajax php里面获取到foreach的内容 response来自于 ajax php 
          $('tbody').append(response);
          start =+ limit;
          getExistingData(start,limit);
        }
      }
    })
  }
//php
if ($_POST['key'] == 'getExistingData') {
    $start = $conn->real_escape_string($_POST['start']);
    $limit = $conn->real_escape_string($_POST['limit']);
    $sql = $conn->query("SELECT id,countryName FROM country LIMIT $start,$limit");

    if ($sql->num_rows > 0) {
      $response = "";
      while($data = $sql->fetch_array()) {
        $response .= '
          <tr>
            <td>'.$data['id'].'</td>
            <td>'.$data['countryName'].'</td>
            <td>
            <input type="button" value="Edit" class="btn btn-primary">
            <input type="button" value="View" class="btn">
            <input type="button" value="DELETE" class="btn btn-danger">
            </td>
          </tr>
        ';
        
      } 
      exit($response);
      
    } else {
        exit('reachedMax');
      }
    // code...
  }

Top