我已经构建了一个搜索表单,我试图将$_POST
数据发送到ajax中的另一个php,但它不起作用。
我想知道,我是否可以将$_POST
数据发送到ajax中的url:"searchby.php"
?
search.php:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" id="searchTerm">
<input type="text" name="search" id="search" autocomplete="off">
<button type="loading" class="btn btn-primary">Add</button>
</form>
<script type="text/javascript">
var ajax_arry=[];
var ajax_index =0;
var sctp = 100;
$(function(){
$('#loading').show();
$.ajax({
url:"searchby.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();
var searchTerm = "<?php echo $x; ?>";
if ((($(window).scrollTop()+document.body.clientHeight)==$(window).height()) && isload=='true'){
$('#loading').show();
var ajaxreq = $.ajax({
url:"searchby.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!");
}
});
});
</script>
我的searchby.php
应该是这样的。
searchby.php:
$searchstring = $_POST['search'];
$sql = "SELECT * FROM product WHERE p_name LIKE '%$searchstring %'";
答案 0 :(得分:1)
您需要在ajax param中发送搜索值:
var searchVal = $("#search").val(); // get value of search box
$.ajax({
url:"searchby.php",
type:"POST",
data:"search="+searchVal+"&actionfunction=showData&page=1",
cache: false,
success: function(response){
$('#loading').hide();
$('#demoajax').html(response);
}
});
更新1:
在表单提交时发送ajax请求,您可以按照以下示例:
$(".btn").click(function(){
var searchVal = $("#search").val(); // get value of search box
$.ajax({
url:"searchby.php",
type:"POST",
data:"search="+searchVal+"&actionfunction=showData&page=1",
cache: false,
success: function(response){
$('#loading').hide();
$('#demoajax').html(response);
}
});
});
在PHP(searchby.php)中调试您在$ _POST中获得的内容:
echo "<pre>";
print_r($_POST);
答案 1 :(得分:0)
您必须在发布请求中发送输入search
值,以检索此值,您可以使用ID选择器#
,例如:
var search = $('#search').val();
$.ajax({
url:"searchby.php",
type:"POST",
data:"actionfunction=showData&page=1&search="+search,
cache: false,
success: function(response){
$('#loading').hide();
$('#demoajax').html(response);
}
});
希望这有帮助。