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>
asked May 15, 2013 at 3:09
iamnards
2172 gold badges8 silver badges18 bronze badges
2 Answers 2
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]);
}
answered May 15, 2013 at 3:25
ramesh
4,09213 gold badges76 silver badges119 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
dcromley
"Avoid comments like "+1" or "thanks", but This is a GOOD POST! Thanks. +1
First
- Id must be unique
- so if you want to use same id then use class instead
Second
name="themes"should bename="themes[]"
Good read
answered May 15, 2013 at 3:10
NullPoiиteя
57.3k23 gold badges130 silver badges148 bronze badges
Comments
default