I do have a table that is generated dynamically from MySQL table values. Each listed record has a checkbox.
list.php
$query="SELECT * FROM drivers ORDER BY name";
$query=$mysqli->query($query);
$queryN=mysqli_num_rows($query);
if($queryN!=0){
while($row=mysqli_fetch_assoc($query)){
$id=$row['id'];
$name=$row['name'];
$output .='<tr id="'.$id.'">
<td>'.$name.'</td>
<td><input type="checkbox" name="assign" class="assigned" value='.$id.'"/></td>
</tr>';
}
}else{
$output .='<tr>
<td colspan="2">No data found</td>
</tr>';
}
The HTML file
<input type="button" name="send" id="send" value="Send" />
Now the JQuery script that will send only the values of the checked checkboxes in the table:
<script>
$(document).ready(function(){
$('.send').click(function(){
var checkValues = $('input[name=assign]:checked').map(function()
{
return $(this).val();
}).get();
$.ajax({
url: 'assigndriver.php',
type: 'post',
data: { ids: checkValues },
success:function(data){
}
});
});
});
</script>
Finally, the PHP file assigndriver.php that will insert the array with the values of only checked checkboxes:
include "../connect_to_mysql.php";
$id = array();
if(isset($_POST['ids'])){
$id = $_POST['ids'];
for ($i = 0; $i < count($id); $i++) {
$id_new = $id[$i];
$sql="INSERT INTO teste (campo) VALUES ('$id_new')";
}
}
Well, I would like to know why it isn't working and if in this way the correct value of the checkbox, that must be the variable $id is being posted correctly to the PHP file.
Thank you