gpt4 book ai didi

javascript - 从 PHP 获取 $_POST 到 Javascript 并将其传递给另一个 php

转载 作者:行者123 更新时间:2023-11-29 07:25:16 27 4
gpt4 key购买 nike

我构建了一个具有无限滚动的搜索功能。如何从搜索页面获取搜索字符串到查询数据库的滚动页面。

PHP:

搜索.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>

滚动.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;
}
?>

Javascript:

搜索.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 ..."

感谢。

最佳答案

<form action="search" method="post">

这会重定向到您没有的 /search 页面。您有 /search.php/scroll.php。我认为您应该将 scroll.php 作为操作,然后您就会得到结果。

<form action="scroll.php" method="post">

编辑:
我似乎误解了代码。 scroll.php 是其中的一个 API 端点。然后操作仍然是 search.php,但是,为了获得提交的值,您可以用 1 block 石头杀死 2 只鸟。

  1. 更新表单 HTML(您不需要操作,也不需要表单,但我们保留它):

    <form action="" method="post">
    <input type="text" name="search" id="search" autocomplete="off">
    <button type="button" id="do_search" class="btn btn-primary">Search</button>
    </form>
  2. 在您的 JS 中,使该函数由搜索触发:

    jQuery(document).ready(function($) {
    $("#do_search").on("click", function() {
    var searchTerm = $("#search").val();

    //this is where you do the AJAX stuff. You can use searchTerm variable.
    });
    });

关于javascript - 从 PHP 获取 $_POST 到 Javascript 并将其传递给另一个 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34757796/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com