dimanche 27 août 2017

Checkbox submits only one checkbox value

I have a form with multiple checkboxes, say A, B, C, D. Sometimes i will want the user to select more than one checkboxes, say B and C. When I do that I will see only the last value 'C' in the database. How will I modify my codes below to have BC submitted to the database?

FORM

<form id="myform" method="post" action="check_trash.php">
<input type="text" name="afam" require />

A<input type="checkbox"name="get_value[]" value="A">  
B<input type="checkbox" name="get_value[]" value="B">  
C<input type="checkbox"name="get_value[]" value="C">  
D<input type="checkbox" name="get_value[]" value="D">

<button id="subm" name="upload" style="color:#fff; padding:5px 66px" class="btn btn-success" ><b>Save</b></button>
</form>

Jquery

$("#subm").click( function() {

$.post( $("#myform").attr("action"), $("#myform").serialize(), function(info){ $("#result").html(info); } );
clearInput();
});

$("#myform").submit( function() {
    return false;
});

function clearInput() {
$('#myform').each(function(){
this.reset();
});    
}

PHP

if(!empty($_POST["get_value"])){
foreach($_POST["get_value"] as $checkbox){
}
$name = $_POST['myname'];
$insert_query = "insert into trash (checkbox,name) values ('$checkbox','$name')";

$run_query = mysqli_query($con, $insert_query);
if($run_query){
    echo "Submitted successfully";
}
else {
    echo "failed";
}
}

else{
echo "<script>alert('Please select at least one option!')</script>";
}




Aucun commentaire:

Enregistrer un commentaire