-1

I'm calling send Array function and I'm hoping to create and array and send it to my PHP file. I want the PHP to extract the data and enter the values into the columns in a mySQL database.

function sendArray(){

             var name = "John";
             var address = "UK"


             var sendInfo = {
                 Name: name,
                 Address: address,

                };


                $.ajax({
                       url: "http://server/~name/folder/insertOffer.php",
                       type: "POST",
                       dataType: "json",
                       data: {data: sendInfo},

                       })
                       .done(function(msg) {
                             if (msg) {
                             alert("Somebody" + name + " was added in list !");
                             location.reload(true);
                             } else {
                             alert("Cannot add to list !");
                             }
                             });

}

<?php

include("mysqlconnect.php");

$sendInfo = file_get_contents('php://input');

$data = [];

$Name = $_POST['data']['name'];
$Address = $_POST['data']['Address'];


mysqli_query($con,"INSERT INTO offerSelected (Id, Url)
VALUES ('$name', '$address')");

?>

1 Answers1

1

You have one problem with your JS:

var sendInfo = {
    Name: name,
    Address: address,
};

Trailing commas can cause problems in some browsers. Remove the comma from after address.

When making the HTTP request you can either:

  • Send JSON or
  • Send form encoded data

(There are other options, but these are the ones relevant to the code you are using)

Your JavaScript is sending form encoded data, but your PHP is trying to perform a weird mix of parsing both.

$sendInfo = file_get_contents('php://input');

Remove the above line. You are sending form encoded data. You don't need to get the raw input for manual parsing. PHP will parse form encoded data automatically and put it in $_POST.

$data = [];

Remove the above line. You never use $data.

$Name = $_POST['data']['name'];

You called the property Name in your JavaScript, not name. Be consistent. Change that to:

$Name = $_POST['data']['Name'];

Then:

$Address = $_POST['data']['Address'];

This should work fine by itself (given the JavaScript you have).

mysqli_query($con,"INSERT INTO offerSelected (Id, Url) VALUES ('$name', '$address')");

You called your variables $Name and $Address. Don't change them to $name and $address in the middle of your program.

Note, however, that once you fix that, you remain vulnerable to SQL injection attacks that you need to defend yourself from.

Community
  • 1
  • 1
Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • I feel my javaScript isn't activating correctly my alert("Somebody" + name + " was added in list !") never functions. I'm running this script in a cordova app so it's difficult for me to view errors. The function is being called correctly however. – Michael O'Neill Sep 11 '14 at 09:41
  • @MichaelO'Neill — Then you need to learn how to use cordova's debugging tools better to figure out what is actually going on. – Quentin Sep 11 '14 at 09:44
  • I've found half a dozen problems with your code and no others leap out at me. – Quentin Sep 11 '14 at 09:49