1

i want to pass array which is generated by javascript..just say i have an array named vals2=('john','peter') and i want to pass this array to my php page(insert_paket_f.php).

this is my ajax code :

$.ajax({
        type: "POST",
        url: "insert_paket_f.php",
        data: { data : vals2 },
        cache: false,
        //vals=('john','peter','andrea');
        success: function(){
            alert("OK");
        }
    });

insert_paket_f.php

$data1 = $_POST['data'];                
$data1 = explode(",", $_POST['data']);
print_r($data1);

when i run my browser, its show empty array, and just looks like this Array ( [0] => )

how can i fix this?

thanks..

1
  • Try: $.ajax({ type: "POST", url: "insert_paket_f.php", data: {'data[]' : {a:'1', b : '2'}}, ........ Commented Nov 26, 2012 at 11:20

3 Answers 3

1

Try this:

Use join to send the javascript array as an string.

Javascript

var vals2 = ['john','peter'];
$.ajax({
    type: "POST",
    url: "insert_paket_f.php",
    data: { data : vals2.join(',') },
    cache: false,
    success: function(){
        alert("OK");
    }
});

PHP

$data1 = $_POST['data'];                
$data1 = explode(",", $_POST['data']);
print_r($data1);

Hope it helps.

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

2 Comments

What do you mean by "my array already contain ',' (coma)"? Please explain so that i can explain it furthur.
If you mean ',' in the join function, i do it to convert the array to an string which is sent to the php and php converts it back to an array using explode.
0

Try:

$data1 = json_decode($_POST['data']);
print_r($data1);

Comments

0

In the Data Feild try to use variables

as echo data: data[]=john&data[]=peter&data[]=andrea

Code:-

$.ajax({
    type: "POST",
    url: "insert_paket_f.php",
    data: "data[]=john&data[]=peter&data[]=andrea",
    cache: false,
    success: function(){
        alert("OK");
    }
});

Is It working?

1 Comment

the array is generated by javascript and populated from dynamic dropdown..so i cant do your code..btw thanks for trying to help me :D

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.