Я создаю функцию поиска с бесконечным прокруткой. Как получить строку поиска со страницы поиска на мою страницу прокрутки для базы данных запросов.
PHP:
search.php
<head> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="style/css/scroll.css"/> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <link href="css/search.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="search"> <form action="search" method="post"> <input type="text" name="search" id="search" autocomplete="off"> <button type="submit" class="btn btn-primary">Search</button> </form> </div> <img id='loading' src='img/loading.gif'> <div id="demoajax" cellspacing="0"> </div> </body> <script type="text/javascript" src="js/infinitescroll/search.js"></script>
scroll.php
<?php include('db.php'); $searchstring = $_POST['search']; if(isset($_REQUEST['actionfunction']) && $_REQUEST['actionfunction']!=''){ $actionfunction = $_REQUEST['actionfunction']; call_user_func($actionfunction,$_REQUEST,$con,$limit); } function showData($data,$con,$limit){ $page = $data['page']; if($page==1){ $start = 0; } else{ $start = ($page-1)*$limit; } $sql = "SELECT fm_product.p_name, fm_product.p_descp, fm_product.p_id, fm_product.p_price, fm_product.p_discount, fm_product.p_img, fm_member.member_display_name, fm_product.p_member_id, fm_package.package_name, fm_package.package_id FROM fm_member LEFT JOIN fm_product ON fm_member.member_id = fm_product.p_member_id LEFT JOIN fm_package ON fm_member.package_id = fm_package.package_id order by p_created_date desc limit $start,$limit"; $str=''; $data = $con->query($sql); if($data!=null && $data->num_rows>0) { while( $row = $data->fetch_array(MYSQLI_ASSOC)){ if($row['package_id']=='1'){ $package = "No Package"; } else { $package = "<form class='form-item'><input name='product_code' type='hidden' value='".$row['p_id']."'><button type='submit'>Add to Cart</button></input></form>"; } $id = $row['p_id']; $str.="<div style=align: center class='data-container'><a href=item?id = $id><img src=upload/".$row['p_img']." width=300px style=max-width:100%; height: auto; vertical-align: middle></a><p>By ".$row['member_display_name']."</p><p>Product Name : ".$row['p_name']."</p><p>Price : ".$row['p_price']."</p><p>Discount : ".$row['p_discount']."</p><p>Description : ".$row['p_descp']."</p><p>Package ".$_POST['search']." : ".$row['package_name']."</p><p>".$package."</p></div>"; } $str.="<input type='hidden' class='nextpage' value='".($page+1)."'><input type='hidden' class='isload' value='true'>"; } else { $str .= "<input type='hidden' class='isload' value='false'><p>Finished</p>"; } echo $str; } ?>
в<?php include('db.php'); $searchstring = $_POST['search']; if(isset($_REQUEST['actionfunction']) && $_REQUEST['actionfunction']!=''){ $actionfunction = $_REQUEST['actionfunction']; call_user_func($actionfunction,$_REQUEST,$con,$limit); } function showData($data,$con,$limit){ $page = $data['page']; if($page==1){ $start = 0; } else{ $start = ($page-1)*$limit; } $sql = "SELECT fm_product.p_name, fm_product.p_descp, fm_product.p_id, fm_product.p_price, fm_product.p_discount, fm_product.p_img, fm_member.member_display_name, fm_product.p_member_id, fm_package.package_name, fm_package.package_id FROM fm_member LEFT JOIN fm_product ON fm_member.member_id = fm_product.p_member_id LEFT JOIN fm_package ON fm_member.package_id = fm_package.package_id order by p_created_date desc limit $start,$limit"; $str=''; $data = $con->query($sql); if($data!=null && $data->num_rows>0) { while( $row = $data->fetch_array(MYSQLI_ASSOC)){ if($row['package_id']=='1'){ $package = "No Package"; } else { $package = "<form class='form-item'><input name='product_code' type='hidden' value='".$row['p_id']."'><button type='submit'>Add to Cart</button></input></form>"; } $id = $row['p_id']; $str.="<div style=align: center class='data-container'><a href=item?id = $id><img src=upload/".$row['p_img']." width=300px style=max-width:100%; height: auto; vertical-align: middle></a><p>By ".$row['member_display_name']."</p><p>Product Name : ".$row['p_name']."</p><p>Price : ".$row['p_price']."</p><p>Discount : ".$row['p_discount']."</p><p>Description : ".$row['p_descp']."</p><p>Package ".$_POST['search']." : ".$row['package_name']."</p><p>".$package."</p></div>"; } $str.="<input type='hidden' class='nextpage' value='".($page+1)."'><input type='hidden' class='isload' value='true'>"; } else { $str .= "<input type='hidden' class='isload' value='false'><p>Finished</p>"; } echo $str; } ?>
Javascript:
search.js
var ajax_arry=[]; var ajax_index =0; var sctp = 100; $(function(){ $('#loading').show(); $.ajax({ url:"scroll.php", type:"POST", data:"actionfunction=showData&page=1", cache: false, success: function(response){ $('#loading').hide(); $('#demoajax').html(response); } }); $(window).scroll(function(){ var height = $('#demoajax').height(); var scroll_top = $(this).scrollTop(); if(ajax_arry.length>0){ $('#loading').hide(); for(var i=0;i<ajax_arry.length;i++){ ajax_arry[i].abort(); } } var page = $('#demoajax').find('.nextpage').val(); var isload = $('#demoajax').find('.isload').val(); if ((($(window).scrollTop()+document.body.clientHeight)==$(window).height()) && isload=='true'){ $('#loading').show(); var ajaxreq = $.ajax({ url:"scroll.php", type:"POST", data:"actionfunction=showData&page="+page, cache: false, success: function(response){ $('#demoajax').find('.nextpage').remove(); $('#demoajax').find('.isload').remove(); $('#loading').hide(); $('#demoajax').append(response); } }); ajax_arry[ajax_index++]= ajaxreq; } return false; if($(window).scrollTop() == $(window).height()) { alert("bottom!"); } }); });
Я хочу получить $_POST['search']
из search.php
чтобы scroll.php
заменил его на WHERE
по этому запросу.
$sql = "SELECT ... FROM ... LEFT JOIN ... ON ... LEFT JOIN ... ON ... WHERE p_name LIKE '%$_POST['search']' ORDER BY ... LIMIT ..."
Оценил.