0

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);
?>
  • try `$name = $_POST['name'];` and `$address = $_POST['address'];` (watch out for the capitals) – Lorenzo Marcon Sep 11 '14 at 13:11
  • PHP variables are case sensitive. Change `$Name` to `$name` and `$Address` to `$address` or the other way around. – Traian Tatic Sep 11 '14 at 13:11
  • I'm still getting undefined index for name and address and I'm getting expects parameter 1 to be mysqli – Michael O'Neill Sep 11 '14 at 13:19
  • why are you using single quotes to wrap variables '$name' and '$address' may be you can use concatenation like: "INSERT INTO offerSelected (Id, Url) VALUES ('".$name."', '".$address."')" <-- like this. – Sagar Guhe Sep 11 '14 at 14:46

3 Answers3

0

Try this :

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 code:

<?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')");
  ?>
S.Thiongane
  • 6,883
  • 3
  • 37
  • 52
  • @MichaelO'Neill as far as the parameter 1 thing goes, that has to do with how you create $connection, which I assume is in mysqlconnect.php. A quick search shows that this is often because the connection is made with mysql instead of mysqli functions. [For example this](http://stackoverflow.com/questions/10160664/mysqli-query-warning-mysqli-query-expects-parameter-1-to-be-mysqli) – lordstyx Sep 11 '14 at 15:58
0

why are you using single quotes to wrap variables '$name' and '$address' change your code to this may be this will help you:

<?php

 include("mysqlconnect.php");


 $name = $_POST['name']; 
 $address = $_POST['address'];


 mysqli_query($connection,"INSERT INTO offerSelected (Id, Url) VALUES ('".$name."', '".$address."')");
  ?>
Sagar Guhe
  • 1,041
  • 1
  • 11
  • 35
0

Try this:

<?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."')");

?>

Where is $connection being defined?

Dan Delaney
  • 353
  • 1
  • 5