I’m creating a page to input my student’s result but it only saved one student’s result instead on all students in the list. I used javascript to auto-save/update input value.
I created a loop for fetch.php and it saves all students at once without using JavaScript. I need help on the script so I can save all students result at once
function autosave() {
var title = $("#post_title").val();
var desc = $("#post_desc").val();
var id = $("#post_id").val();
if (title != " " && desc != " ") {
$.ajax({
url: "fetch.php",
method: "POST",
data: {
send_title: title,
send_desc: desc,
send_id: id
},
dataType: "text",
success: function(data) {
if (data != "") {
$("#post_id").val(data);
}
$("#autosave").text("Data saved");
} /* success */
}) /* ajax */
} /* filter */
} /* autosave */
setInterval(function() {
autosave();
setInterval(function() {
$("#autosave").text("");
}, 5000)
}, 10000)
<?php $results = mysqli_query($conn,"SELECT * FROM student LIMIT 5");
while($rows = mysqli_fetch_array($results)) { ?>
<tr>
<td>
<?php echo $rows['fname'].' '.$rows['lname']?>
</td>
<td><input type="text" class="form-control" id="post_title" name="post_title[]"></td>
<td><input type="text" class="form-control" id="post_desc" name="post_desc[]"></td>
<input type="hiden" class="form-control" value="<?php echo $rows['idnumber'] ?>" id="post_id" placeholder="hidden ID value" name="post_id[]">
<?php } ?>
<div id="autosave"></div>
About is the index file that call for all students and I can input score for each. Below is also fetch.php to save data into db.
$N = count($st);
for($i=0; $i < $N; $i++) {
$st = $_POST["send_id"];
$fa1 = $_POST["send_title"][$i];
$fa2 = $_POST["send_desc"][$i];
$r = mysqli_query($conn, "SELECT * FROM tbl_data WHERE idnumber='$st[$i]'");
// GET THE NUMBER OF ROWS RETURNED:
$rows = @mysqli_num_rows($r);
if ($rows === 0) {
$sql = "INSERT INTO tbl_data (post_title, post_desc, idnumber)
VALUES ('".$_POST["send_title"]."','".$_POST["send_desc"]."','$st[$i]')";
mysqli_query($conn, $sql);
//echo mysqli_insert_id($conn);
} else{
$sql = "UPDATE tbl_data SET post_title='".$_POST["send_title"]."',
post_desc='".$_POST["send_desc"]."' where idnumber = '$st[$i]'";
mysqli_query($conn, $sql);
}
2
Answers
You can collect your
form-control
data into an array:and then pass this
data
when you call your$.ajax
.Explanation:
tr
elements insideform
elements. You may want to adjust the selectortr
push
it intodata
The problem in your current code is that you get unique elements by id. Instead, we need to find all rows.
Further notes:
id
s, they are meant to be unique$_POST["send_id"]
to$_POST["send_id"][$i]
, so you will save the changes for the correct userYou are including HTML nodes with repeated IDs in your DOM, which may lead to inconsistent data when working with JavaScript. It’s important to avoid using duplicate IDs on a same DOM.
To achieve what you want in a simpler and more organized manner, I recommend using a
<form>
wrapped around your<table>
and then serializing the form using the jQueryserializeArray
function. This way, you can easily collect the form data and send it as the data payload in your AJAX request to the backend.For instance, you can name your input fields in the form as follows:
When you submit the form, the data will be serialized in a structured manner based on the input names, making it easier to handle in the backend. For example, on the backend, you will receive the data somewhat like this:
This will allow you to process the data efficiently on the server-side and avoid complications due to duplicate IDs in the DOM.