将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);
?>
答案 0 :(得分:0)
试试这个:
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')");
?>
答案 1 :(得分:0)
为什么使用单引号来包装变量&#39; $ name&#39;和&#39; $地址&#39;将您的代码更改为此可能会对您有所帮助:
<?php
include("mysqlconnect.php");
$name = $_POST['name'];
$address = $_POST['address'];
mysqli_query($connection,"INSERT INTO offerSelected (Id, Url) VALUES ('".$name."', '".$address."')");
?>
答案 2 :(得分:0)
试试这个:
<?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?