Javascript 在另一页上显示搜索数据<;部门>;
我有一个搜索栏在Javascript 在另一页上显示搜索数据<;部门>;,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我有一个搜索栏在index.php页面上搜索。但是我想在culops.php上显示结果。如何在AJAX中实现这一点 注意:#search_result是coupons.php文件的一个div,我想在其中显示结果 AJAX $("document").ready(function() { $("#search_form").on("submit", function (e) { e.preventDefault();
index.php
页面上搜索。但是我想在culops.php
上显示结果。如何在AJAX中实现这一点
注意:#search_result
是coupons.php文件的一个div,我想在其中显示结果
AJAX
$("document").ready(function() {
$("#search_form").on("submit", function (e) {
e.preventDefault();
$.post("result.php?query=" + encodeURIComponent($("#list_search").val()), function (data) {
var res = JSON.parse(data);
if (res.divs) {
$('#search_result').html("");
for (var i = 0; i < res.divs.length; i++) {
$('#search_result').append(res.divs[i]);
}
} else {
$('#search_result').html("No matched coupons found !");
}
});
});
});
</script>
$(“文档”).ready(函数(){
$(“#搜索表格”)。关于(“提交”,功能(e){
e、 预防默认值();
$.post(“result.php?query=“+encodeURIComponent($(“#list_search”).val()),函数(数据){
var res=JSON.parse(数据);
如有(res.divs){
$(“#搜索结果”).html(“”);
对于(变量i=0;i
我假设您需要以某种方式在另一个页面上显示结果,但不能使用服务器端。我不知道您为什么要这样做,但ajax肯定不是选项,如果您不能使用服务器端,您可以这样做。而不是在couples.php页面上发送JSON响应。将搜索查询发送到优惠券页面。因此,您应该在index.php中创建一个类似这样的搜索表单
<form action="coupons.php" method="GET">
<input type="text" name="query" />
<input type="submit" value="Submit" />
</form>
然后在优惠券.php页面中,您可以通过window.location.search访问您的搜索查询,这将为您提供用户搜索的查询
现在使用这个查询,在这里的coupons.php页面上,您将编写逻辑来获取json响应,并将结果打印在#search_result元素中
因此,您的优惠券.php将类似于
$("document").ready(function() {
var q = window.location.search; // q is your query string you got from index.php
$.post('result.php'+q, function (data) {
var res = JSON.parse(data);
if (res.divs) {
$('#search_result').html("");
for (var i = 0; i < res.divs.length; i++) {
$('#search_result').append(res.divs[i]);
}
} else {
$('#search_result').html("No matched coupons found !");
}
});
});
$(“文档”).ready(函数(){
var q=window.location.search;//q是从index.php获得的查询字符串
$.post('result.php'+q,函数(数据){
var res=JSON.parse(数据);
如有(res.divs){
$(“#搜索结果”).html(“”);
对于(变量i=0;i
我假设您需要以某种方式在另一个页面上显示结果,但不能使用服务器端。我不知道您为什么要这样做,但ajax肯定不是选项,如果您不能使用服务器端,您可以这样做。而不是在couples.php页面上发送JSON响应。将搜索查询发送到优惠券页面。因此,您应该在index.php中创建一个类似这样的搜索表单
<form action="coupons.php" method="GET">
<input type="text" name="query" />
<input type="submit" value="Submit" />
</form>
然后在优惠券.php页面中,您可以通过window.location.search访问您的搜索查询,这将为您提供用户搜索的查询
现在使用这个查询,在这里的coupons.php页面上,您将编写逻辑来获取json响应,并将结果打印在#search_result元素中
因此,您的优惠券.php将类似于
$("document").ready(function() {
var q = window.location.search; // q is your query string you got from index.php
$.post('result.php'+q, function (data) {
var res = JSON.parse(data);
if (res.divs) {
$('#search_result').html("");
for (var i = 0; i < res.divs.length; i++) {
$('#search_result').append(res.divs[i]);
}
} else {
$('#search_result').html("No matched coupons found !");
}
});
});
$(“文档”).ready(函数(){
var q=window.location.search;//q是从index.php获得的查询字符串
$.post('result.php'+q,函数(数据){
var res=JSON.parse(数据);
如有(res.divs){
$(“#搜索结果”).html(“”);
对于(变量i=0;i
我的朋友,有什么问题吗?如果你需要另一个页面上的结果,为什么你需要ajax?只需提交并根据您的帖子数据选择查询并显示结果。您能澄清一下吗?您想在index.php上搜索,然后在couples.php上显示吗?那么你就不需要AJAX了。或者,您想在coupons.php页面上使用相同的功能?只需包含相同的javascript。正如罗伊所问,你的实际问题是什么?@delboy1978uk是的,我正在index.php上搜索一些关键字,然后当我点击按钮时,它会在优惠券.phpdiv
上显示结果,idsearch\u result
ah,那么你想将另一个文件加载到div中吗?我认为在jQuery中应该使用.load()<代码>$('#搜索结果')。加载('tuople.php')或者,您可能需要执行$.post()并将任何变量发送到页面,然后执行$('search_result').html(数据)代码>ajax调用返回的html数据在哪里?我的朋友,有什么问题吗?如果你需要另一个页面上的结果,为什么需要ajax?只需提交并根据您的帖子数据选择查询并显示结果。您能澄清一下吗?您想在index.php上搜索,然后在couples.php上显示吗?那么你就不需要AJAX了。或者,您想在coupons.php页面上使用相同的功能?只需包含相同的javascript。正如罗伊所问,你的实际问题是什么?@delboy1978uk是的,我正在index.php上搜索一些关键字,然后当我点击按钮时,它会在优惠券.phpdiv
上显示结果,idsearch\u result
ah,那么你想将另一个文件加载到div中吗?我认为在jQuery中应该使用.load()<代码>$('#搜索结果')。加载('tuople.php')
或者,您可能需要执行$.post()并将任何变量发送到页面,然后执行$('search_result').html(数据)代码>其中数据是ajax调用返回的html刚刚尝试了您的代码并对其进行了调试。它在var q=window.location.search行上给出q=“?query=gucci”