试图将数组从前端发布到php。连接打开但不发送数据。

时间:2017-11-02 14:39:16

标签: javascript php arrays ajax

所以我从API捕获数据并将其存储在如下所示的数组中:stackVar[{user, red, 12}, {user1, green, 13}]

然后我试图用_POST发送它。测试后我意识到我实际上正在保存数组,因为console.line(stackVar)工作。我还注意到我打开了与php文件的连接,但它在将任何数据发布到php页面之前关闭了连接。这是我的前端的样子。

if(results.length == stackVar.length){

var request = new XMLHttpRequest();
request.open('POST', 'http://www.server.com/saveF.php', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.send(JSON.stringify(stackVar));
}

和我的后端。

<?php   $conn = new mysqli($sn, $un, $pw, $db);

if ($conn->connect_error) {
    die("connection failed: " . $conn->connect_error);
}



$bInfo = $_POST["stackVar"];
$infoEncoded = json_encode($bInfo);
$getsome = $infoEncoded[0][0];
$sql = "INSERT INTO companies(company) VALUES ('$getsome')";
echo $getsome;



if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}


$conn->close()
?>

0 个答案:

没有答案