Reputation: 3504
I am trying to pass multiple arrays to my script and from there I will insert these array values in database. In Branches_Control.php
I want to save these array values in PHP variables and then pass them to my model and loop through that PHP array variable to insert
them in the DB.
This is my AJAX request:
var finalplaces = [];
var finallongArr = [];
var finallatArr = [];
var finalphone = [];
var finalcompanyName = [];
for (var i = 0; i < places.length; i++) {
if (finalplaces[i] != null) {
finalplaces[i].push(places[i])
finallongArr[i].push(longitudeArr[i]);
finallatArr[i].push(latitudeArr[i]);
finalphone[i].push(phone[i]);
finalcompanyName[i].push(companyName[i]);
}
}
$.ajax({
type: 'POST',
url: "Branches_Control.php",
data: "Places=" + finalplaces +
"&longitudeArr=" + finallongArr +
"&latitudeArr=" + finallatArr +
"&phones=" + finalphone +
"&companyNames=" + finalcompanyName,
success: function(data) {
alert(data + "Succese");
document.getElementById("asd").innerHTML = data;
}
});
What I am trying to do in my Branches_Control.php
$places = $_POST['Places'];
echo $places[0];
Can anyone tell me why this does not work?
Upvotes: 0
Views: 2862
Reputation: 6333
you error is here:
data: "Places=" + finalplaces +
"&longitudeArr=" + finallongArr +
"&latitudeArr=" + finallatArr +
"&phones=" + finalphone +
"&companyNames=" + finalcompanyName,
if you send data as string than you have to stringify these arrayes with, example:
"&longitudeArr=" +JSON.stringify(finallongArr )
and then on server side you have to make a json decode:
$longitudeArr= json_decode($_POST['longitudeArr'])
Or more simply with jquery power you can pass data as object do this:
data:{longitudeArr:finallongArr , latitudeArr: finallatArr , phones: finalphone .....}
the advatages of this last method is that on php site, you have immediately the array:
$longitudeArr= $_POST['longitudeArr'] //this is an array
Try.
Upvotes: 2
Reputation: 91478
In your code:
var finalplaces = [];
for (var i = 0; i < places.length; i++) {
if (finalplaces[i] != null) {
finalplaces[i].push(places[i])
}
}
the condition if (finalplaces[i] != null)
can't be true
because the array is empty, so you're never populating the array.
I guess you want to reverse the test: if (finalplaces[i] == null)
Upvotes: 1