1

would like to ask if its possible/how to pass array variable in php using javascript

i have this following code

jQuery("#save").click(function() {


    var val = [];
    jQuery('#themes:checked').each(function(i){
      val[i] = jQuery(this).val();
    });



    jQuery("#status").load("<?=$config['publicdomain']?>/saveProfile.php?wthemes="+val);


});

this is the form field

<span>
    <input id="themes" name="themes" type="checkbox" value="theme a">
    <input id="themes" name="themes" type="checkbox" value="theme b">
</span>
iamnards
  • 217
  • 2
  • 7
  • 18

2 Answers2

1

First

Second

  • name="themes" should be name="themes[]"

Good read

jQuery AJAX POST example

Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
1

Your input should be

<input type="hidden" id="car" name="vehicle[]" value="car" />
<input type="hidden" id="bike" name="vehicle[]" value="bike" />
<input type="hidden" id="truck" name="vehicle[]" value="truck" />
<input type="hidden" id="cycle" name="vehicle[]" value="cycle" />
<input type="hidden" id="train" name="vehicle[]" value="train" />

then in server side

<?php

$vehicles=$_POST["vehicle"];

foreach($vehicles as $vehicle){
 // Do your sfuff here
}

?>

If using ajax this may help you

$.ajax({
    url: "index.php",
    type: 'POST',
    data: form_data,
    dataType:"json",
    success: function(data) {
        alert(data[0]);
   }
ramesh
  • 4,008
  • 13
  • 72
  • 117