[英]jquery not sending button choice to php
我有一個搜索函數,它將接受搜索字符串並將其發送到php文件以解析數據庫列。 我也希望用戶選擇他們想要搜索的網站的哪個方面(漫畫,藝術品或兩者)。 漫畫和藝術品或存儲在兩個單獨的表中。
這是一個接受來自下面的html的輸入搜索字符串並將其發送到php文件的函數。
<script type="text/javascript">
function search(searchString) {
//var site = $("#site").val();
$.get("./scripts/search.php", {_input : searchString},
function(returned_data) {
$("#output").html(returned_data);
}
);
}
這是javascript接受搜索“漫畫”,“藝術品”或“所有”的選擇。
function searchChoice(choice) {
alert("Choice: " + choice);
$.get("./scripts/search.php", {_choice : choice}
);
}
</script>
HTML:
<!--Search filtering for comics, artwork, or both-->
<span class="search"><b>Search for: </b> </span>
<div class="btn-group" data-toggle="buttons-radio">
<span class="search">
<button type="button" class="btn" id="comics" onclick="searchChoice(this.id)">Comics</button>
<button type="button" class="btn" id="artwork" onclick="searchChoice(this.id)">Artwork</button>
<button type="button" class="btn" id="all" onclick="searchChoice(this.id)">All</button>
</span>
</div>
<br/>
<br/>
<!--Search functionality-->
<span class="search">
<input type="text" onkeyup="search(this.value)" name="input" value="" />
</span>
<br />
<span id="output"><span class="sidebarimages"> </span></span>
PHP摘錄:
$input = (isset($_GET['_input']) ? ($_GET['_input']) : 0);
$siteChoice = (isset($_GET['_choice']) ? ($_GET['_choice']) : "all");
你可以看到javascript在選擇漫畫按鈕時正確警告“選擇:漫畫”,但是php端, echo "</br>Choice: " . $siteChoice;
echo "</br>Choice: " . $siteChoice;
,是回聲“全部”,這是不正確的。
任何想法將不勝感激!
正如提到的@E_p,這就是問題......另一種選擇是創建變量並將數據存儲在那里...試試這個:你不需要改變html
var mySearchString = 0;
var myChoice = 'all';
function search(searchString) {
mySearchString = searchString;
GetSearch();
}
function searchChoice(choice) {
myChoice = choice;
GetSearch();
}
function GetSearch(){
$.get("./scripts/search.php", {_input : mySearchString, _choice : myChoice},
function(returned_data) {
$("#output").html(returned_data);
}
);
}
你沒有為_choice保持狀態。
調用搜索時,它不會將其傳遞給服務器。
您需要將按鈕更改為選項,並在搜索功能中通過兩者。 同時到服務器
用單選按鈕替換按鈕並使用form.Serialize()
<form id="searchform">
<input type="radio" name="_choice" value="comics" />Comics<br/>
<input type="radio" name="_choice" value="artwork" />Artwork<br/>
<input type="radio" name="_choice" value="all" />All<br/>
<input type="text" onkeyup="search()" name="_input" value="" />
</form>
使用Javascript
function search() {
//var site = $("#site").val();
$.get("./scripts/search.php", $('#searchform').serialize(),
function(returned_data) {
$("#output").html(returned_data);
}
);
}
.serialize()
函數將表單輸入轉換為JSON,因此您不必手動輸入,不再需要參數,也不需要兩個函數,只需一個來完成所有操作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.