Madh Madh - 25 days ago 14
jQuery Question

Selected checkboxes are not checked. why?

checkbox.php

<body>

<form>

<label>
<input type="checkbox" value="1" name="name">fruits</label>
<label>
<input type="checkbox" value="2" name="name">vegitables</label>

</form>

<!--modal-->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">

</div>
<div class="modal-footer">
<button type="submit" class="btn btn-default" id="submit" onclick="submit();">save</button>
<h4 id="result"></h4>

</div>
</div>
</div>
</div>



</body>


Above is my main two checkboxes. A modal appears when I click on a checkbox. On which displays dynamic checkboxes(sub checkboxes) from database. when I click on sub checkboxes on modal and then click submit. Alert pops with no values. what I need to do is to get checked checkboxes values into alert. can anyone help me?

Here are my checkbox.js

$(document).ready(function(){
$("input[type=checkbox][name=name]").change(function(){
if(this.checked) {

var value = $(this).val();
$.ajax({
url:"modal.php",
type:"POST",
data:{value:value},
success:function(modalBody){
$("#myModal .modal-body").html(modalBody);
$("#myModal").modal('show');
}

});
}
});




});


function submit() {

var values = [];
$('input[type=checkbox][name=sub]').each(function(){
if($(this).is(":checked"))
{
values.push($(this).val());
alert(values.join());
}
});

}


and modal.php

<html>
<body>

<?php

if($_POST['value']){
$test = $_POST['value'];
include("config.php");

$sql = "SELECT name FROM tb where Id=".$value." ";
$res = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($res)){
?>
<input id='category' type='checkbox' name=sub'[]' value = <?php echo $row['name']; ?> /><?php
echo $row['name'];
echo "<br>";

}

}

else echo "not post";

?>

</body>
</html>


config.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "test";


// Create connection
$conn = mysqli_connect($servername, $username, $password,$db);


// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully <br/>";
?>

Answer

Fix your php page

    <?php   
             if($_POST['value']){
                   $test = $_POST['value'];
                   include("config.php");

                $sql = "SELECT name FROM tb where Id=".$value." ";
                $res = mysqli_query($conn,$sql);
                while($row = mysqli_fetch_assoc($res)){
                echo "<input class='category' type='checkbox' name='sub[]' value ='".$row['name'];."'/>";
                echo $row['name'];
                echo "<br>";

        }

    }

else echo "not post";

?>

your js

  var values = [];
     $('.category:checked').each(function(){
           values.push($(this).val()); 
        });
    alert(values.join());       
Comments