0
我有一個HTML選擇,用戶選擇一個選項,然後單擊去。接下來我想發生的事情是,根據他們在第一次選擇中選擇的選項,從另一個mysql數據庫接收數據。需要幫助使用php和jquery ajax填充從mysql數據庫的數據填充html選擇
我有我的代碼工作到它將運行查詢獲取數據,然後在ajax調用中運行成功。問題是我不知道如何格式化返回的數據,以便能夠使用它填充html選擇。我想我需要解析返回到數組中的結果,該數組可以用來輸出每個值的選項。但我不知道如何做到這一點。或者如果有人有更好的建議,請幫助。
process.php
<?php
$pdo = new PDO("mysql:host=localhost;dbname=sales_rep", "root", "");
$country = $_POST['q'];
$stmt = $pdo->prepare("
SELECT DISTINCT state_prefix
FROM zips
WHERE country = '$country'
ORDER BY state_prefix ASC
LIMIT 50");
if($stmt->execute()){
echo json_encode($stmt->fetchAll(PDO::FETCH_OBJ));
} else {
echo "query fail";
}
?>
rep.js
$('#country').submit(function(event) {
event.preventDefault();
/*clear result div*/
$("#result").html('');
var values = $(this).serialize();
$.ajax({
url: "process.php",
type: "post",
data: values,
dataType: 'json',
success: function(response){
console.log(response);
},
error:function(){
alert("failure");
$("#result").html('there is error while submit');
}
});
});
既然你'json_encode'ing您的數據和在'success'處理器期待JSON,沒有必要分析結果。你能告訴我們什麼'console.log(response);'輸出嗎? – Steve 2013-03-13 22:49:52