Fetch values from a table using jquery on basis of a checkbox update
By : Liviu Naghi
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I'd suggest giving each td element with an associated value a class, so that you can use jQuery to grab the value. Suppose we gave the class name of 'value'. Then, you could probably try something like this: code :
$('checkbox').on('click', getValue);
function getValue() {
var value = $(this).closest('tr').find('.value').text();
}
|
How to show checkbox checked/unchecked on the basis of bool variable fetched from database
By : Naresh Akkarapaka
Date : March 29 2020, 07:55 AM
I hope this helps you . You just need to set the Model/ViewModel property to the value which the variable hold in the controller and the strongly typed helper method will take care and will decide either to check the checkbox or not depending on the value in it. In controller action set the property value on the basis of whatever your business logic is and pass the model from controller to view. code :
model.SFLandRFListAttached = SomeMethodReturningBoolean();
return View(model);
@Html.CheckBoxFor(f => f.SFLandRFListAttached)
|
How to update values of checkbox to the database 1 if checked and 0 if unchecked?
By : Win7Develop
Date : March 29 2020, 07:55 AM
I wish this help you Please go though documentation and understand the way how the forms works. There are plenty of examples out there I would recommend Head First book series and here you can find a good example. And also here is the example code for your problem code :
<html>
<head>
<!-- link to jquery lib -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<form action="" method="POST">
<h4 id="result"></h4>
<div class="container">
<h2 align="center">Table App Feature</h2>
<label> Name </label>
<input type='text' name='name' id='name' value=''/>
<table id="appFeature" class="table table-hover" align="center" style="width:500px;margin:auto;">
<thead>
<tr>
<th>Firstname</th>
<th>Please check to enable the features</th>
</tr>
</thead>
<tbody>
<tr>
<td>Smarthome</td>
<td>
<!-- checkbox for smarthome value -->
<input type="checkbox" class="get_value" id='smarthome'/>
</td>
</tr>
<tr>
<td>Intercom</td>
<td>
<input type="checkbox" class="get_value" id='intercom'/>
</td>
</tr>
</tbody>
</table><br />
<div align="center">
<label>check if you want to update, unckeck if you want to insert</label>
<input type="checkbox" class="get_value" id='update'/>
<br>
<!-- button name -->
<button type="button" name="submit" id="submit">Update or Insert</button>
</div>
</div>
</form>
</body>
<script type="text/javascript">
$(document).ready(function(){
$('#submit').click(function(){
// get the value is checked from the form
$.ajax({
url: "insert.php",
method: "POST",
data:{intercom: $('#intercom').is(':checked'),smarthome: $('#smarthome').is(':checked'), name: $('#name').val(), update: $('#update').is(':checked')},
success:function(data){
$('#result').html(data);
}
});
});
});
</script>
</html>
<?php
$servername = "localhost";
$username = "YOURDBUSER";
$password = "YOURDBPASSWORD";
$dbname = "databaseappfeature"; // db name
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql ;
if($_POST['update']){
$sql = "UPDATE appfeature SET smarthome=".$_POST['smarthome'].", intercom=".$_POST['intercom']." WHERE name='".$_POST['name']."'";
}else{
$sql = "INSERT INTO appfeature (name, smarthome, intercom) VALUES ('".$_POST['name']."',".$_POST['smarthome'].",".$_POST['intercom'].")";
}
if ($conn->query($sql) === TRUE && !$_POST['update']) {
echo "New record created successfully";
}else if($conn->query($sql) === TRUE && $_POST['update']){
echo "record updated";
}else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
|
How to add/remove item on the basis of checkbox checked or unchecked?
By : richa
Date : March 29 2020, 07:55 AM
will be helpful for those in need I have written below lines of code , Why don't simply use map() and get() on the checked check boxes: code :
var selectedFinal = [];
//On click check all records
$(document).on("click", "#completebatch,.commoncheckbox", function () {
selectedFinal = $('.commoncheckbox:checked').map((i,el) =>{
return $(el).attr('value')
}).get();
console.log(selectedFinal);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<thead>
<th><input onclick="checkenabledisable()" id="completebatch" type="checkbox"></th>
</thead>
<tbody>
<tr>
<td><input class="commoncheckbox disabled-check" value="578" type="checkbox" disabled=""></td>
<td>abc</td>
</tr>
<tr>
<td><input class="commoncheckbox" value="357" type="checkbox"></td>
<td>abc</td>
</tr>
<tr>
<td><input class="commoncheckbox" value="123" type="checkbox"></td>
<td>abc</td>
</tr>
</tbody>
</table>
|
How to append and remove ids on the basis of checkbox checked unchecked in html table?
By : podda
Date : March 29 2020, 07:55 AM
wish helps you I think the simple way is to use replace maybe there is a better solution than this but it's not in my head right now code :
$(document).on("change", ".commoncheckbox", function() {
var studentIds = $("#allStudentIds").val().trim();
$("#tagsOfStudents").show();
$("#empty_row").remove();
var id = $(this).val();
var generatedString;
if ($(this).is(":checked")) {
if (studentIds == "") {
generatedString = id;
} else {
generatedString = studentIds + "," + id;
}
var uniqueList = generatedString.split(',').filter(function(item, i, allItems) {
return i == allItems.indexOf(item);
}).join(',');
$("#allStudentIds").val(uniqueList);
var $td = $(this).closest('tr').children('td');
var name = $td.eq(2).text();
var gender = $td.eq(4).text();
var className = $td.eq(3).text();
var deleteButton = '<button title="Delete" onclick="deleteRow(\'' + id + '\')" class="btn btn-danger table_btn btn-outline btn-sm"><i class="fa fa-trash"></i></button>';
var html = '<tr id=row_' + id + '>' + '<td>' + name + '</td>' + '<td>' + className + '</td>' + '<td>' + gender + '</td>' + '<td>' + deleteButton + '</td>' + '</tr>';
if ($('#row_' + id).length > 0) {} else {
$("#selected_students").append(html);
}
} else {
var removeId = studentIds.replace( ','+id+',' , '').replace( ','+id , '').replace( id+',' , '').replace(id , '');
$("#allStudentIds").val(removeId);
if ($('#row_' + id).length > 0) {
$("#row_" + id).remove();
}
}
}
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<thead>
<th><input onclick="checkenabledisable()" id="completebatch" type="checkbox"></th>
<th></th>
...
</thead>
<tbody id="studentListBody">
<tr>
<td><input class="commoncheckbox disabled-check" value="578" type="checkbox" disabled=""></td>
<td>abc</td>
<td>abc</td>
<td>abc</td>
<td>abc</td>
</tr>
<tr>
<td><input class="commoncheckbox" value="357" type="checkbox"></td>
<td>abc</td>
<td>abc</td>
<td>abc</td>
</tr>
<tr>
<td><input class="commoncheckbox" value="123" type="checkbox"></td>
<td>abc</td>
<td>abc</td>
<td>abc</td>
<td>abc</td>
</tr>
</tbody>
</table>
<input type="text" value="" id="allStudentIds">
|