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. What am I doing wrong?
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 (for example, 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;
});