我有上有一個搜索表單如下搜索PHP頁面調用jQuery函數(我已經刪除了自舉類)從表單前一頁
<form id="searchform" name="search" role="form" action="/Search" method="post">
<input type="text" class="form-control" name="keywords" placeholder="keywords e.g. some words here"/>
<input type="hidden" id="location" value="" class="county" name="location[]" />
<button class="btn btn-primary btn-lg" type="submit" name="search_button" id="searchbutton">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
SEARCH
</button>
</form>
在此搜索頁面我有頂部顯示相同的搜索表單,結果顯示在右側列表中,左側顯示覆選框列表並選擇稱爲AJAX並優化搜索的列表。調查結果被加載到名爲displaylisting的div
中。
$(document).ready(function() {
$('# list here comma separated').on('change',function(){
$.ajax({
type: "POST",
url: "ajaxlistings.php",
contentType: 'application/x-www-form-urlencoded',
data: $("#formID").serialize(),
cache: false,
dataType: 'html',
success: function(data){
console.log(data);
$('#displaylisting').html(data);
},
error: function (xhr, status, error) {
alert("Sorry, there was a problem!: " + error);
console.log(xhr.status);
console.log(xhr.responseText);
console.log(error);
},
complete: function (xhr, status) {
//$('#showresults').slideDown('slow')
}
});
return false;
});
/* Search button clicked */
$("#searchbutton").click(function() {
$.ajax({
type: "POST",
url: "ajaxlistings.php",
contentType: 'application/x-www-form-urlencoded',
data: $("#formID").serialize(),
cache: false,
dataType: 'html',
success: function(data){
console.log(data);
$('#displaylisting').html(data);
},
error: function (xhr, status, error) {
alert("Sorry, there was a problem!: " + error);
console.log(xhr.status);
console.log(xhr.responseText);
console.log(error);
},
complete: function (xhr, status) {
//$('#showresults').slideDown('slow')
}
});
return false;
});
當在search.php頁面上測試時,所有這些工作都正常。
這裏是我的問題:
從邏輯上講,去這個搜索頁面,在我的index.php我也有同樣的搜索表單。
我想要的是,當用戶點擊我的index.php頁面上的搜索表單時,它們被帶到search.php頁面(它目前正在執行的操作),但我想調用AJAX調用這裏顯示列表。
這可能嗎?作爲解決方法,我在主頁上創建了一個隱藏變量,並將其設置爲1,當提交到搜索PHP頁面時,它會調用ajaxlisting.php中相同的PHP代碼,當此值爲1時。但似乎反作用,特別是當ajaxlisting.php會幫我完成這項工作。
我有搜索論壇,但無法找到我的問題的答案。
此線程是非常接近我想要的沒有任何示例(http://stackoverflow.com/questions/37999208/how-to-submit-form-on-one-page-to-a-different-page-使用-Ajax的通話中的jquery?RQ = 1) –