![](/img/trans.png)
[英]How can I send values in the body in the HTTP GET request in Angular?
[英]How to send an array to a PHP file so the values can be added to mySQL using HTTP request?
將javaScript數組發送到PHP文件,然后將元素存儲在mySQL數據庫中。
目前,我的“ httpSend.responseText”警報出現錯誤。 聲明:未定義索引:..行中的名稱8
注意:未定義的索引:第9行中..中的地址
警告:mysqli_query()期望參數1為mysqli,第12行在..中給定的資源
我不確定該數組是正確發送還是被正確接收。
var name = "John";
var address = "UK";
var sendInfo = {
Name: name,
Address: address
};
var httpSend = new XMLHttpRequest();
var php = "http://server/~name/folder/insertOffer.php";
httpSend.open("POST", php, true);
httpSend.onreadystatechange = function() {//Call a function when the state changes.
if(httpSend.readyState == 4 && httpSend.status == 200) {
alert(httpSend.responseText);
}
}
httpSend.send(sendInfo);
的PHP
<?php
include("mysqlconnect.php");
$name = $_POST['name'];
$address = $_POST['address'];
mysqli_query($connection,"INSERT INTO offerSelected (Id, Url) VALUES ('$name', '$address')");
?>
mysqlconnect看起來像這樣
<?php
$connection = mysql_connect("localhost", "user", "pass");
if(!$connection){
die('Could not connect to server: ' . mysql_error());
}
mysql_select_db("database", $connection);
?>
嘗試這個 :
var name = "John";
var address = "UK";
var sendInfo = {
Name: name,
Address: address
};
var params = "sendInfo=" + JSON.stringify(sendInfo);
var httpSend = new XMLHttpRequest();
var php = "http://server/~name/folder/insertOffer.php";
httpSend.open("POST", php, true);
httpSend.onreadystatechange = function() {//Call a function when the state changes.
if(httpSend.readyState == 4 && httpSend.status == 200) {
alert(httpSend.responseText);
}
}
httpSend.send(params);
PHP代碼:
<?php
include("mysqlconnect.php");
$sendInfo = json_decode($_POST['sendInfo']);
$name = $sendInfo ['name'];
$address = $sendInfo ['address'];
mysqli_query($connection,"INSERT INTO offerSelected (Id, Url) VALUES ('$name', '$address')");
?>
為什么使用單引號將變量“ $ name”和“ $ address”包裝起來,將代碼更改為此,這可能會幫助您:
<?php
include("mysqlconnect.php");
$name = $_POST['name'];
$address = $_POST['address'];
mysqli_query($connection,"INSERT INTO offerSelected (Id, Url) VALUES ('".$name."', '".$address."')");
?>
嘗試這個:
<?php
include("mysqlconnect.php");
$name = $_POST['Name']; // NOTE THE CASE CHANGE HERE AS THIS IS WHATS DEFINED IN YOUR JS
$address = $_POST['Address'];
mysqli_query($connection,"INSERT INTO offerSelected (Id, Url) VALUES ('".$name."','".$address."')");
?>
$ connection在哪里定義?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.