Reputation: 35
I want to get the value of the id in the checkbox. but the variable in id always give the 1st value. how can send php array to jquery array. thanks
require "connection.php";
$sql = mysqli_query($con,"SELECT * FROM position ORDER BY position_align + 0 ASC");
$switch = array();
$pid = array();
while($row = mysqli_fetch_assoc($sql)){
$pcode = $row['position_code'];
$pdesc = $row['position_desc'];
$status = $row['status'];
?>
<tr>
<td><?php echo $pcode; ?></td>
<td><?php echo $pdesc; ?></td>
<td>
<div class="custom-control custom-switch">
<?php
if($status == 'active'){
?>
<div class="onoffswitch4">
<input type="checkbox" name="onoffswitch4" class="onoffswitch4-checkbox switch" onclick="change(this.value)" id="myonoffswitch" checked value="<?php echo $pcode; ?>">
<label class="onoffswitch4-label" for="pid[]">
<span class="onoffswitch4-inner"></span>
<span class="onoffswitch4-switch"></span>
</label>
</div>
<?php
}else{
?>
<div class="onoffswitch4">
<input type="checkbox" name="onoffswitch4" class="onoffswitch4-checkbox switch" onclick="change(this.value)" id="aaa[]" value="<?php echo $pcode; ?>">
<label class="onoffswitch4-label" for="myonoffswitch">
<span class="onoffswitch4-inner"></span>
<span class="onoffswitch4-switch"></span>
</label>
</div>
<?php
}
?>
$('.switch').on('change', function() {
if(this.checked) {
var id = this.value;
$.ajax({
url: "admin_change_status.php",
data: {
id:id
},
type: "POST",
success: function(result){
}
});
}else{
var id2 = this.value;
$.ajax({
url: "admin_change_status.php",
data: {
id2:id2
},
type: "POST",
success: function(result){
}
});
}
});
i expect to get the value of id in checkbox to jquery.
Upvotes: 0
Views: 70
Reputation: 1661
If you want to get all values, you can only get it by classname not by id. You have to loop though all classes names and get each one of them, you can do this by each function. Each function loop though all the elements.
$('.onoffswitch4-checkbox').each(function(i){
var statsValue = $(this).val();
console.log(statsValue);
});
Upvotes: 3