简体   繁体   English

PHP没有收到ajax发布数据

[英]php not receiving ajax post data

I'm trying to send data to a php file through AJAX. 我正在尝试通过AJAX将数据发送到php文件。 I can get it to work fine with the GET method, but it's not working with the POST method (my final file could get big, so I want to use POST). 我可以使它与GET方法一起正常工作,但不能与POST方法一起工作(我的最终文件可能会变大,所以我想使用POST)。

Javascript 使用Javascript

function update_table() { var mydrop = document.getElementById('dropdown').value; 函数update_table(){var mydrop = document.getElementById('dropdown')。value;

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

} }

my PHP 我的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>

I put the print_r function in there just to double check, and POST is empty not matter what I try. 我把print_r函数放在那里只是为了仔细检查,而POST是空的不管我尝试什么。

Note I thought that maybe I needed header('Content-Type: text/xml') but when I put that in, the page doesn't work at all. 注意,我以为我可能需要header('Content-Type:text / xml'),但是当我把它放进去时,该页面根本无法工作。

Thanks, 谢谢,

Try 尝试

Request1.setRequestHeader("Content-type","application/x-www-form-urlencoded");
Request1.send("brand=" + mydrop);

And set die after printing $_POST. 并在打印$ _POST之后设置模具。 Check out in chrome dev-tools what kind query you make to php. 在chrome dev-tools中查看您对php进行哪种查询。

you must forget the famous header ajax json for jquery or another
(only necessary for php but essenssial for a good response serveur)
<?PHP
$data = /** whatever you're serializing **/;
header('Content-Type: application/json');
echo json_encode($data);

**https://stackoverflow.com/questions/4064444/returning-json-from-a-php-script**

( if errors, you must write this first, before one echo html.. (and no echo html is good for great json response)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM