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>
0

2 Answers 2

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]);
   }
Sign up to request clarification or add additional context in comments.

1 Comment

"Avoid comments like "+1" or "thanks", but This is a GOOD POST! Thanks. +1
1

First

Second

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

Good read

jQuery AJAX POST example

Comments

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.