John Davidson

hide a row of table filled dynamically with a button click php-jquery

0 comments
Message:


good evening, I have dynamically filled my table from the database. at the end of each row I have a button.
my idea is : when i clicked the button the row is hidden. I succes to recuperate the id of each row but jquery code does not work and my console doesn't return any error. i don't know where the problem is? or even if the process is correct.


also,at the latest i hope to replace the button with a checkbox but i don't know how to apply my idea with a checkbox


   <table name="table" class="table" border="1">
<tbody>
<?php
include 'controller.php';
$controller1 = new Controller();
$res = $controller1->array();

while ($donne = $res->fetch()) {
?>
<tr id="<?php $donne['id'] ?>">
<td> <?php echo $donne['operateur'] ?></td>
<td> <?php echo $donne['machine'] ?></td>
<td> <?php echo $donne['declaration'] ?></td>
<td>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post' style="display: inline;">
<input type='hidden' id='id' name='id' value="<?php echo $donne['id']; ?>" />
<input type='submit' name='validate' id='validate' value='validate' />

</form>

</td>
</tr>
<?php
}
?>
</table>

and this is my jQuery code


 if (isset($_POST['id']) && !empty($_POST['id'])) {
?>
<script>
$(document).ready(function () {
$("#validate").click(function () {
var i = $("#id").val();
var i = i.toString()
$("#" + i).hide();
});
});
</script>

<?php
}


My Answer:
Here is an example of how you can hide a row of a table filled dynamically with a button click using PHP and jQuery:

1. PHP code to dynamically fill the table with data from the database:

php
// Fetch data from the database
// Assuming $rows is an array of rows fetched from the database

echo '';
foreach ($rows as $row) {
echo '';
echo '';
echo '';
echo '';
echo '';
}
echo '
' . $row['column1'] . '' . $row['column2'] . '
';
?>


2. jQuery code to hide the row when the button is clicked:

javascript
$(document).ready(function() {
$('.hide-row').click(function() {
var rowId = $(this).data('row-id');
$('#row_' + rowId).hide();
});
});


3. To replace the button with a checkbox, you can modify the PHP code to generate checkboxes instead of buttons:

php
echo ' Hide Row';


And then modify the jQuery code to hide the row when the checkbox is checked:

javascript
$(document).ready(function() {
$('.hide-row').change(function() {
if ($(this).is(':checked')) {
var rowId = $(this).data('row-id');
$('#row_' + rowId).hide();
}
});
});


Make sure to include jQuery library in your HTML file for the jQuery code to work.

Rate this post

3 of 5 based on 5457 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us