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);
}