Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to send a JavaScript array via JQuery AJAX to a new PHP array:

var heart = [31,32,33,34,35,36,37,38,39,42,43];

$('#find_btn').click(function(){
             $.ajax({
            type: "POST",
            url: 'scripts/get_specimens_system.php',
            data: {
                    system: heart,
            },
            success: function(data) {
$('#search_list_container').html(data);
                }   
});
});

In the php script I have the following before a MySQL query:

$system = array($_POST['system']);

If I hardcode the data in heart like:

$system = array(31,32,33,34,35,36,37,38,39,42,43);

the query works OK. Any ideas what I'm doing wrong?

UPDATE

In an attempt to simplify the question I have left out the fact that I'm setting the JS array to var system via system = $(this).attr('data-system');

Where the data-system is set to the arrays like:

$('#sub_category_box_systems').html("<a class='sub_category_link' href='#' data-subcat-type='system' data-system='" + brain + "'>Brain</a><br /><hr><a class='sub_category_link' href='#' data-subcat-type='system' data-system='" + nerves + "'>Nerves</a><br /><hr><a class='sub_category_link' href='#' data-subcat-type='system' data-system='" + cerebellum + "'>Cerebellum</a>");

Now if I alert system just before the AJAX statement the alert shows the intergers fine.

$('#sub_category_box_systems').on('click', '.sub_category_link', function() {       
    system = $(this).attr('data-system');   
});


$('#find_btn').click(function(){
        alert(system);
             $.ajax({
            type: "POST",
            url: 'scripts/get_specimens_system.php',
            data: {
                    system: system,
            },
            success: function(data) {

}
});
});

AND I have updated the PHP file to:

$system = $_POST['system'];

Still no go, though if I swap the var system with the actual var of an array (eg heart), as the AJAX data it works. So what's wrong here? (sorry for changing this OP...).

Odd, if I change var system to a specific array like below it works, so there must be some problem with the formatting using the jquery .attr('data-system');

$('#sub_category_box_systems').on('click', '.sub_category_link', function() {   
    system = heart; 
});
share|improve this question

4 Answers 4

up vote 2 down vote accepted

You're adding an extra array around the parameter, which is already an array. Just do:

$system = $_POST['system'];
share|improve this answer
    
Or if trying to send a multi-dimensional array, use $system[0] –  nwolybug Sep 4 '14 at 2:28
    
Only if he just wants the first row of it. –  Barmar Sep 4 '14 at 2:28
    
In an attempt to simplify the question I have omitted the fact that I'm getting the JS array via an html data tag and using system = $(this).attr('data-system'); Should I update the OP or open a new question? –  IlludiumPu36 Sep 4 '14 at 2:43
1  
Where the array comes from is irrelevant. An array is an array. –  Barmar Sep 4 '14 at 2:44
    
@Barmar, yup. $system[0] would only pull the first index of the multi-dimensional array. In this case, it is what the OP would need since we see no other information. –  nwolybug Sep 4 '14 at 3:52

By default, jQuery $.ajax will submit your request as form data. If you open your browser debugger while the request is sent, you would see this request data:

system[]:31
system[]:32
system[]:33
system[]:34
system[]:35
system[]:36
system[]:37
system[]:38
system[]:39
system[]:42
system[]:43

PHP understands that this is an array and reads it in as an array by default. You should not wrap it in an array() tag.

You can assign the array to your own variable as such: $system = $_POST['system'];

However, your work isn't quite done there. $system is now an array of strings. Here is a var_dump of $system:

array(11) {
  [0]=>
  string(2) "31"
  [1]=>
  string(2) "32"
  [2]=>
  string(2) "33"
  [3]=>
  string(2) "34"
  [4]=>
  string(2) "35"
  [5]=>
  string(2) "36"
  [6]=>
  string(2) "37"
  [7]=>
  string(2) "38"
  [8]=>
  string(2) "39"
  [9]=>
  string(2) "42"
  [10]=>
  string(2) "43"
}

Use this to convert it to an array of integers:

$system = array_map('intval', $system);

share|improve this answer
    
It's not really necessary to convert them to integers, since PHP will usually convert strings to numbers when necessary. –  Barmar Sep 4 '14 at 2:46
    
The pitfalls of relying on the loosely typed variables are many. I'm not advocating strong typing for a simple web app, but it certainly is good practice to keep your numbers set as numbers and your strings set as strings. If I were the OP, I would've changed the data type of the POST to application/json so it's clear to PHP that his data is either numeric or a string. –  Elliot B. Sep 4 '14 at 2:49

I awarded the answer to @Barmar as his solution solved the original OP. I still had a problem with getting the JS arrays across to the PHP array due to using system = $(this).attr('data-system'). Changing the code to the following fixed that issue:

$('#sub_category_box_systems').on('click', '.sub_category_link', function() {       
    system = $(this).attr('data-system').split(",");   
});
share|improve this answer

you can convert that to a string by saying

data: {
  system: heart.join(",")
 }

and converting back to array on the server side by saying

$system = explode(",",$_POST['system']);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.