使用AJAX和PHP可以獲得多個響應嗎?使用AJAX和PHP獲取multipe響應
事件由選擇框觸發,當它改變值(onchange
)時,它調用JavaScript函數並獲取xmlhttp.responseText
。
但是在responseText
我們只能根據id更改一個值/ innerHtml。 我想使用AJAX更改2個或更多HTML。可能嗎?
這裏是我的AJAX功能:
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState != 4 && xmlhttp.status == 200) {
document.getElementById("txtStockQTY"+baris).value = "Validating..";
}
else if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtStockQTY"+baris).value = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "ajaxGetStock.php?id="+id, true);
xmlhttp.send();`
而且ajaxGetStock
如下:
$idbarang = $_GET['id'];
include "../connect.php";
$resultSetStockType = mysql_query("SELECT STOCK_QTY,STOCK_QTYUNIT,STOCK_SIZE,STOCK_SIZE2 FROM TSTOCK WHERE STOCK_ID = $idbarang",$con);
if($resultSetStockType and mysql_num_rows($resultSetStockType) > 0) {
while($rowSetStockType = mysql_fetch_array($resultSetStockType)) {
echo $rowSetStockType['STOCK_QTY']." ".$rowSetStockType['STOCK_QTYUNIT'];
}
}
else {
echo "--- stok tidak ditemukan ---";
}
我想從我的AJAX獲得幾個返回值,並將其分配給文本類型的多個輸入。
請勿使用mysql_ *方法,因爲它們已被廢棄並且自PHP 7起已被刪除。 – Ikari
我建議你使用像「JSON」這樣的格式化文本:http://www.w3schools.com/js/js_json.asp – DiD
**危險**:您正在使用[an **過時的**數據庫API](http: //stackoverflow.com/q/12859942/19068)並且應該使用[現代替換](http://php.net/manual/en/mysqlinfo.api.choosing.php)。你很容易受到[SQL注入攻擊](http://bobby-tables.com/)**現代的API會使它更容易[防禦](http://stackoverflow.com/questions/60174/最好的方式,以防止SQL注入在PHP)自己從。 – Quentin