我寫這段代碼,但它不起作用。我想用ajax在php中顯示一個數組。 這是一個html選擇,選擇該選項的每個選項的值選項取入變量並將其發送到ajax。 Ajax應該將數據發佈到php,然後php從數據庫中選擇接收到的數據並顯示它們。 但我無法在ajax中顯示此數據。 :(與ajax後,並顯示recived數據
$(function(){
$("#topic").change(function(){
var str = "";
$("select option:selected").each(function() {
str += $(this).text() + " ";
options(str);
});
});
});
function options(option){
$.ajax({
type: "POST",
dataType: 'json',
url: "/Register/checkSelect", //Relative or absolute path to response.php file
data: {
option:option
}).done(function(){
$("#content").html(data);
alert("ok");
});
});
}
你忘了張貼你的PHP代碼 – Raja 2015-04-06 12:56:53
抱歉,這是PHP鱈公共職能checkSelect(){ $ getTopic = $ _POST [ 「主題」]; $分貝=新分貝(); $結果= $ DB->查詢( 「SELECT * FROM主題WHERE主題= '$ getTopic'」); foreach($ result as $ topic){ echo $ topic ['keyword']; } } – Amin 2015-04-06 12:59:29
在'done'回調中沒有'data'參數。另外'html()'將取代所有。請使用瀏覽器控制檯/開發工具來檢查錯誤 – charlietfl 2015-04-06 12:59:51