我是AJAX的新手,非常抱歉,如果這很簡單,但我已經看過其他所有的地方,看不到我做錯了什麼。我在做這個AJAX請求有什麼問題?
當我將$ _POST ['name']換成實際值時,例如 - $ name ='apple'; - 一切正常。但是,當我嘗試從下面的php文件中的AJAX post請求中獲取值時,它不起作用。幫幫我!提前致謝。
PHP
$name = $_POST['name'];
$sql = "SELECT * FROM sales WHERE email = 'test' AND item = '$name' ";
$result = mysqli_query($conn, $sql);
if ($result){
$row = mysqli_fetch_array($result);
echo json_encode($row);
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
jQuery的
$(document).ready(function(){
$('.option').on('click', function(){
var name = $(this).val();
$.post('ajax.php', {name: name}, function(data) {
var obj = jQuery.parseJSON(data);
var desc = obj[7]
$('#name').val(desc);
});
});
});
HTML
Item: <input type = "text" id = "name" size = "6">
Description: <input type = "text" id = "name" size = "6">
Qty: <input type = "text" id = "name" size = "6">
Price: <input type = "text" id = "name" size = "6">
Discount: <input type = "text" id = "name" size = "6">
Account: <input type = "text" id = "name" size = "6">
Tax Rate: <input type = "text" id = "name" size = "6">
Amount: <input type = "text" id = "name" size = "6">
<div class = "option">Apple</div>
檢查您使用var_dump($ _ POST ['name'])獲得了正確的結果; – scaisEdge
你可以發佈的HTML代碼以及 –
你有任何錯誤?還是什麼呢? – Condorcho