0

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?

4
  • I hope that there's $_POST in original code. If not, this is the problem you're looking for. Any way use print_r($_POST); and take a look what's inside. Commented May 26, 2012 at 11:26
  • it is returning this Array ( [Places] => [longitudeArr] => [latitudeArr] => [phones] => [companyNames] => ) the values are not there can you tell me why? Commented May 26, 2012 at 11:30
  • 1
    Check if values are set in javascript and they are not null before sending. Commented May 26, 2012 at 11:33
  • thnx alot solved the problem.The problem was in the javascript you are right. Commented May 26, 2012 at 11:43

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.