简体   繁体   中英

How can i pass a php variable to ajax

I have a auto load page and i need to be able to retrieve the data based on a variable as that variable must bring back a specific value. The code below is based on retrieving all the data.But i only need a select few which is based on the $list

Page.php

<?php
  <div class="page-main ">
  $query="SELECT * FROM page WHERE page_id='$list'"; 
  $counting="SELECT * FROM page WHERE page_id='$list'";
  $rows=mysqli_query($connection,$counting);
  $rows_counts=mysqli_num_rows($rows);
  $results=mysqli_query($connection,$query);
  confirm_query($results);
?>

 <div class="loader">
 <img src="loader.gif" alt="loading gif"/>

 </div> 


</div> <!--close page main -->

here is the jquery passing to ajax (it is on same page)

$(document).ready(function(){

$('.loader').hide();  

var load=0;

$.post("ajax.php",{load:load},function(data){  // somehow i need to pass $list to here



      $('.page-main').append(data);


     }); // close ajax
$(window).scroll(function(){


   if($(window).scrollTop() == $(document).height() - $(window).height())
   {
        $('.loader').show();

     load++; 

     $.post("ajax.php",{load:load},function(data){

      $('.page-main').append(data);

  $('.loader').hide();
     }); // close ajax

   };


 });// close window.scroll


});// close document.ready

this is ajax.php ( now here i am getting undefined variable $list, i need to pass $list i am not sure how to pass this $list from php to jquery to ajax.

    $load=htmlentities(strip_tags($_POST["load"])) * 6;

$query="SELECT * FROM page WHERE page_id='$list' ORDER BY page_id DESC  LIMIT ".$load.",6";
$result=mysqli_query($connection,$query);
  confirm_query($result);

// after this while loop ect

try setting $list to a javascript variable . like:

var list=<?php echo $list?>;

then pass it the way you are passing var load .

A possible solution:

<script type="text/javascript">
  var load=0;
  $.ajax({
    type: 'POST',
    url: 'ajax.php',
    data: ({load: load, list: <?php echo $list ?>}),
    success: function(data) {
      $('.page-main').append(data);
    }
  });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM