I have a problem with my ajax code... I have a list of checkbox, the idea is insert the data of the marked checkboxes into the database.
If I select only one check box, the insert is correct. But if I select for example 3, all data appears on the same field, as unique value.
This is the example:
HTML
<input type="checkbox" class="get_value" value="test1">test1</br>
<input type="checkbox" class="get_value" value="test2">test2</br>
<input type="checkbox" class="get_value" value="test3">test3</br>
<input type="checkbox" class="get_value" value="test4">test4</br>
<input type="checkbox" class="get_value" value="test5">test5</br>
<button type='button' name='submit' id="submit">Submit</button>
This is the ajax code
<script>
$(document).ready(function(){
$('#submit').click(function(){
var insert = [];
$('.get_value').each(function(){
if($(this).is(":checked")){
insert.push($(this).val());
}
});
insert = insert.toString();
$.ajax({
url: "./functions/add_list.php",
method: "POST",
data:{insert:insert},
}
});
});
});
</script>
And this one the PHP that do the insert into the database:
if(isset($_POST["insert"])){
$query = 'INSERT INTO music (name,username) VALUES ("'.$_POST["insert"].'","'.$username.'")';
$result = mysqli_query($conn, $query);
}
For example if I check "test1" and "test2", I will see in my MySQL "name" field "test1,test2". But I need see them in diferent fields, not in the same.
I have tested the "foreach ($_POST["insert"] as $value) { insert... } but it did not help me.
Someone have an idea about my error?
I appreciate your help so much. Regards,