1
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">×</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>
は、上記の私のメインの2つのチェックボックスです。チェックボックスをクリックするとモーダルが表示されます。データベースからの動的チェックボックス(サブチェックボックス)が表示されます。私はモーダルでサブチェックボックスをクリックして、サブミットをクリックします。アラートは値なしでポップします。私がする必要があるのは、チェックボックスのチェック値を警告にすることです。誰でも助けてくれますか?ここで
私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());
}
});
}
と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/>";
?>
。ありがとう:D – casper