我試圖通過AJAX發送數據到php文件。我可以使用GET方法正常工作,但它不適用於POST方法(我的最終文件可能變大,所以我想使用POST)。php不接收ajax發佈數據
的Javascript
功能update_table(){ VAR mydrop =的document.getElementById( '下拉菜單')值。
Request1 = new XMLHttpRequest();
if (Request1) {
var RequestObj1 = document.getElementById('Target1');
Request1.onreadystatechange = function() {
if (Request1.readyState == 4 && Request1.status == 200) {
// document.getElementById('Target1').innerHTML = "test";
RequestObj1.innerHTML = Request1.responseText;
}
}
Request1.open("POST", "table.php");
Request1.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
// var url = "table.php?brand="+mydrop;
// Request1.open('GET', url); // this works fine
Request1.send("brand" + mydrop);
} // end Request1 function
}
我的PHP
<?php
// header('Content-Type: text/xml')
ini_set('display_errors',1);
error_reporting(E_ALL);
if(isset($_POST['brand'])) {
$brandAccess = $_POST['brand'];
}
if(isset($_GET['brand'])) {
$brandAccess = $_GET['brand'];
}
print_r($_POST);
print_r($_GET);
include('./includes/connection.inc.php');
$conn = dbConnect();
$sql = "SELECT * from finished_goods WHERE BrandDesc LIKE '{$brandAccess}%' ";
$result = $conn->query($sql);
?>
<table>
<?php foreach ($result as $row) { ?>
<tr>
<td><?php echo $row['ProductNo'] ?></td>
<td><?php echo $row['ProductName'] ?></td>
<td><?php echo $row['BrandDesc'] ?></td>
<td><?php echo $row['QtyOnHand'] ?></td>
</tr>
<?php } ?>
</table>
我把print_r的功能在那裏只是爲了仔細檢查,和POST是空不要緊我做的嘗試。
注意我想我可能需要標題('Content-Type:text/xml'),但是當我放入時,頁面根本不起作用。
感謝,
請求是否出現在控制檯中?嘗試'Request1.send({品牌:「品牌」+ mydrop});' – DanFromGermany