Sending a javaScript array to a PHP file and then storing the elements in a mySQL database.
Currently I'm getting errors in for my "httpSend.responseText" alert. Saying Notice: Undefined index: name in .. line 8
Notice: Undefined index: address in .. on line 9
Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in .. on line 12
I'm not sure if the array is sending correctly or being received correctly.
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 looks like this
<?php
$connection = mysql_connect("localhost", "user", "pass");
if(!$connection){
die('Could not connect to server: ' . mysql_error());
}
mysql_select_db("database", $connection);
?>