iamnards
iamnards

Reputation: 217

how to Pass array variable in PHP POST?

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>

Upvotes: 1

Views: 2849

Answers (2)

NullPoiиteя
NullPoiиteя

Reputation: 57312

First

Second

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

Good read

jQuery AJAX POST example

Upvotes: 1

ramesh
ramesh

Reputation: 4082

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]);
   }

Upvotes: 1

Related Questions