Possible Duplicate:
send arrays of data from php to javascript
I'm sending an Array with ajax to a php file. The Array is a list of names which should be stored in a database. In order to doublecheck this, I tried to echo the sql_query
created in the php file. But the response is always
Invalid argument supplied for foreach()...
So I searched SO for some solutions, and often the answer was something like "your 'Array' is not an Array". So I echoed the Array submitted to the php, which returns all passed names on one line (what I think means that the Array arrives in the php-file).
So here's my JS...
var tourneyData = {
tName : tourneyName,
tNum : number,
tNames : names, // this is an array
tInt : international,
tLig : liga,
tStars : stars
};
$.ajax({
type: "POST",
url: "php/storeTourney.php",
data: tourneyData,
datatype: "json",
success: function(response){
alert("response " + response);
}
});
... and PHP code
$tName = $_POST['tName'];
$tNum = $_POST['tNum'];
$tNames = $_POST['tNames'];
$tInt = $_POST['tInt'];
$tLig = $_POST['tLig'];
$insert = "";
foreach($tNames as $d){ // line pointed in the error message
$insert += "INSERT INTO NAMES VALUES('test', '".$d."');";
}
echo $insert;
I do not exactly understand what's wrong in the code. What could possibly be wrong in the foreach()
-statement, if $tNames
obviously is an Array?