r/PHPhelp • • Dec 09 '24
help how to get the checkboxes data when my table head and body are in separate php files.
How to Fetch ROWID of Checked Rows and Update Database on Button Click?
Hi everyone,
I'm working on a project where I need to verify certain rows in a table. Here's what I want to achieve:
- The user selects rows by checking the checkboxes.
- When the "Verify" button is clicked, the
ROWIDof the checked rows should be fetched and sent to the server - The server will use the
ROWIDto update the corresponding rows in the database. - my code is currently displaying the right data and output. except i when i select row checkbox and click verify, i get ; LOCALHOST SAYS: no rows selected!
- how do i fix this
Buttons:
<div class="row">
<div class="col-sm-8">
<button type="button" class="btn btn-success" id="verify" name ="verify" onClick="verify();">Verify</button>
<button type="button" class="btn btn-danger" onclick="reject()">Reject</button>
</div>
</div>Table:
<table class="table table-bordered" id="table" data-toggle="table" data-query-params="queryParams" data-pagination="true" data-side-pagination="server" data-url="data.php">
<thead class="table-light">
<tr>
<th data-checkbox="true" ></th>
<th>date</th>
<th>type</th>
<th>ID</th>
<th>Name</th>
<th>About</th>
<th>Invitation</th>
</tr>
</thead>
</table>my failed javascript:
document.getElementById('verify').addEventListener('click', function() {
let selectedRowIds = [];
document.querySelectorAll('.row-checkbox:checked').forEach(function(checkbox) {
selectedRowIds.push(checkbox.getAttribute('data-id'));
});
if (selectedRowIds.length > 0) {
console.log("Selected ROWIDs: " + selectedRowIds.join(", "));
let xhr = new XMLHttpRequest();
xhr.open("POST", "action.php?action=VERIFY", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText); // Success or failure message from server
}
};
xhr.send("IDBIL=" + selectedRowIds.join("|"));
} else {
alert("No rows selected!");
}
});data.php
this is how i get my data
$dataStmt = $objConn->query($dataQuery);
// Prepare data for response
$data = [];
while ($row = $dataStmt->fetch(PDO::FETCH_ASSOC)) {
// Fetch NAMA
$namaQuery = "SELECT NAMA FROM PAYROLL.PAYMAS_MAJLIS WHERE KP_BARU = '{$row['IDPENGGUNA']}' AND KATEGORI = '1'";
$namaResult = $objConn->query($namaQuery);
$namaRow = $namaResult->fetch(PDO::FETCH_ASSOC);
$NAMA = $namaRow["NAMA"] ?? 'Unknown';
// Fetch JENIS_MSYT
$jenisQuery = "SELECT JENIS_MSYT FROM PAYROLL.JENIS_MESYUARAT WHERE KOD = '{$row['JENIS_MSYT']}'";
$jenisResult = $objConn->query($jenisQuery);
$jenisRow = $jenisResult->fetch(PDO::FETCH_ASSOC);
$jenis = $jenisRow["JENIS_MSYT"] ?? 'Unknown';
$data[] = [
"<input type='checkbox' class='row-checkbox' data-id='" . htmlspecialchars($row['IDBIL']) . "'>",
$row['IDBIL'],
$jenis,
$row['NO_PEKERJA'],
$NAMA,
$row['PERKARA'],
$row['JEMPUTAN']
];
}action.php
$action = $_GET["action"];
if ($action == "verify") {
$IDBIL = $_POST['IDBIL']; // Fetching ROWID from the POST request
$arr_stringID = explode("|", $IDBIL); // Split the string into an array of ROWID values
//if ($arr_stringID) {
// Loop through each selected ROWID and update the record
foreach ($arr_stringID as $rowid) {
$sqlkm="SELECT * FROM PAYROLL.KEHADIRAN_MESYUARAT WHERE ROWID = '".$rowid."' ";
$resultsqlkm= $objConn->query($sqlkm);
while($rowchka = $resultsqlkm->fetch(PDO::FETCH_ASSOC)){
// Sanitize the ROWID to avoid SQL injection
$rowid = htmlspecialchars($rowid, ENT_QUOTES, 'UTF-8');
// Prepare the update query
$sqlupdt = "UPDATE PAYROLL.ATTENDANCE SET DATE = SYSDATE, VERIFY = 'Y' WHERE ROWID = '$rowid'";
// Execute the query directly without bindParam
$stmt = $objConn->prepare($sqlupdt);
$stmt->execute(); // Execute the update for each ROWID
}
echo 'success'; // Return success message
}
echo 'No rows selected!'; // If no rows were selected
}enlighten me. thank you.
note that im using oracle
0
Upvotes
2
u/MateusAzevedo Dec 09 '24
In which step are you having problems? Is it the PHP or JS code?
Also, can you describe better what the issue is?
1
u/equilni Dec 10 '24 edited Dec 10 '24
This is PHP Help, not Javascript help. You likely want r/learnjavascript to get this part corrected.
Just a note -
click verifyYou have this in your HTML -
onClick="verify();"and then a separate eventlistener for click. If you have averifyfunction as well, you may be messing something up... that's not shown so and error perhaps?For the PHP side:
Turn on error reporting, there's a link in the sidebar that could help you out.
Does this run on it's own - ie can you isolate this code and run it separately from the rest of the application?
It would help to validate this.
Are you getting past this section? Consider
===vs==You are assuming something is here
Then this shouldn't run (nor the rest of the block)
You commented out the line...
So should this still run?
Prepared statements please
This should have been a prepare/execute/fetchAll. Same section in the side that has a link...
You could just use forearch vs while here. Also, is this 1 result or multiple? fetch is one, fetchAll is multiple. Didn't you split the string before???
Did you get to this part?
No, we don't do this. htmlspecialchars is for output
Sadly, that's not what's happening.
If using mySQL, SYSDATE is a function
But don't put the variable in execute....
How does this work with multiple requests?
How do you know no rows were selected? This is outside of the foreach loop
in MVC, data could be the section to... get the data.... not display it...
Considering this is the first line and you didn't share what's prior, I won't go further...