我正在開發基於AJAX的搜索,這是它的演示。我面臨着返回結果中的問題。我需要顯示結果2次。但它只展示過一次。下面是我的HTML代碼通過AJAX,PHP和MYSQL傳遞參數時返回多個值
<form action="" method="post" id="demoform">
<select style="width:250px;padding:5px 0px;color:#f1eedb;" name="product" class="product">
<option>TENNIS</option>
<option>FOOTBALL</option>
<option>SWIMMING</option>
</select>
</form>
<div id="result">Display Result Here</div>
我使用下面的腳本的Ajax檢索數據: -
$(".product").change(function(){
$.ajax({
type : 'POST',
url : 'post.php',
dataType : 'json',
data: {
product : $(".product option:selected").text(),
},
success : function(data){
$('#result').removeClass().addClass((data.error === true) ? 'error' : 'success')
.html(data.msg).show();
if (data.error === true)
$('#demoForm').show();
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
$('#result').removeClass().addClass('error')
.text('There was an error.').show(500);
$('#demoForm').show();
}
});
});
的post.php中的文件具有下面的代碼: -
<?php
require('connect.php');
$get_select = $_POST[product];
if($get_product!='FOOTBALL'){
$return['error'] = true;
return['msg'] = 'Incorrect Selection';
echo json_encode(return);
}
else {
$return['error'] = false;
$i=0;
while($i<2) {
return['msg'] = $get_product;
}
echo json_encode(return);//Returns only one result.
}
?>
我需要顯示結果兩次爲「CRICKET CRICKET」,但它只顯示一次。 我該怎麼做才能得到兩個結果。
我有幾個關於PHP的問題:(1)$ get_product不存在 - 你的意思是$ get_select? (2)所有那些你說'返回'的地方你是不是指'$ return'? (3)你爲什麼要將$ get_product放入$ return ['msg']兩次 - 你認爲你會得到兩個結果嗎?如果你選擇'足球',你應該回到{'error':'false','msg':'FOOTBALL'}。 –
@ adwitya-media看看答案並標記正確的答案:) –