<?php
$con = mysql_connect("localhost","root","");
$db = mysql_select_db("abc");
if(!$db){
echo 'Could Not connect database';
}
$sel = mysql_query("select * from user_registration");
?>
<script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('input[type="checkbox"]').click(function(){
//how to select multiple checkbox value here??????????
});
});
</script>
<div>
<?php
while($row = mysql_fetch_array($sel)){
?>
<label>
<input type="checkbox" name="<?php echo $row['user_id']; ?>" value="<?php echo $row['user_id']; ?>">
<?php echo $row['first_name']; ?>
</label>
<?php
}
?>
</div>
<div class="display"></div>
chaitu11 0 Junior Poster
Dani AI
Generated
If your checkboxes are created from the DB and may change over time, use delegated events and collect the values from only the checked boxes. Also, name them as an array so PHP receives all IDs at once.
<!-- wrap your generated checkboxes -->
<div id="userList">
<!-- each checkbox should be: name="user_id[]" value="<id>" -->
</div>
<div class="display"></div> // show selected IDs in .display whenever any user checkbox changes
var $list = $('#userList');
$list.on('change', 'input[type=checkbox][name="user_id[]"]', function () {
var ids = $list
.find('input[type=checkbox][name="user_id[]"]:checked')
.map(function () { return this.value; })
.get();
$('.display').text(ids.length ? ids.join(', ') : 'None selected');
}); For select-all, avoid inline handlers and prefer .prop() over .attr() so the UI stays in sync (building on ’s idea but with a scoped, unobtrusive handler):
<label><input type="checkbox" id="selectAll"> Select all</label> $('#selectAll').on('change', function () {
var checked = this.checked;
$list.find('input[type=checkbox][name="user_id[]"]').prop('checked', checked).trigger('change');
}); On submit, PHP gets an array of IDs because of the [] name. Also, mysql_* is long deprecated; switch to PDO/mysqli. Example with PDO:
// POST: user_id[]=1&user_id[]=3&user_id[]=7
$ids = isset($_POST['user_id']) ? array_map('intval', (array)$_POST['user_id']) : [];
if ($ids) {
$pdo = new PDO('mysql:host=localhost;dbname=abc;charset=utf8mb4','root','',[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
$in = str_repeat('?,', count($ids) - 1) . '?';
$stmt = $pdo->prepare("SELECT * FROM user_registration WHERE user_id IN ($in)");
$stmt->execute($ids);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
} Notes:
- Use https for your jQuery CDN.
- Give each checkbox a unique
idand pair labels withforfor better accessibility.
Ajay Gokhale 0 Newbie Poster
Hi,
Please find following Javascript functions to select / deselect checkbox using div (This will apply checkboxes in a div)
<script type="text/javascript">
function checkedall(divname) {
$("#" + divname).find(':checkbox').each(function () {
$(this).attr('checked', true);
});
}
function uncheckedall(divname) {
$("#" + divname).find(':checkbox').each(function () {
$(this).attr('checked', false);
});
}
</script>
You can all above functions like
<a href="javascript:void(0);" onclick="javascript:checkedall('div_nave');">Select All</a>
In short you can use following code for single checkbox using Id
$('#checkbox_id').attr('checked', true);
If you have any query, Please let me know.
Thanks,
Ajay
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.