I trying to do in place editing on a table column with ajax jquery. I'am returning the table with ajax jquery like this:
Form to find table:
<div class="input-group">
<span class="input-group-addon">Term</span>
<!-- I passed the tables name as values for a specific period -->
<select name="term" class="form-control" id="term">
<option value="">Select Period</option>
<option value="period_one">1st Period</option>
<option value="period_two">2nd Period</option>
<option value="period_three">3rd Period</option>
<option value="period_four">4th Period</option>
<option value="period_five">5th Period</option>
<option value="period_six">6th Period</option>
<option value="first_exam">First Semester Exam</option>
<option value="second_exam">Second Semester Exam</option>
</select>
</div>
JQuery Ajax script that sends post value:
<script>
$(document).ready(function() {
$('#term').on('change', function() {
var term = $('#term').val();
if (term != '') {
$.ajax({
url:"findGrades.php",
method:"post",
data:{"term":term},
dataType:"text",
success:function(data){
$("#result").html(data);
//after the result have been displayed the datatable function is called
$('#dataTable').DataTable();
}
});
} else {
$("#result").html('');
}
});
});
Codes from my findGrades.php
$term = mysqli_escape_string($connection, $_POST['term']);
$output .= '<td contenteditable="true" onBlur="saveToDatabase(this,'.$term.','.$row["score"].','.$row['id'].')">'.$row["score"].'</td>';
Based upon the result that was return(a table) I want to be able to edit a column in that table, which is the column I've shown here. That's why I added a contenteditable attribute on it.
Ajax is bringing the right result for the table, but when I tried to update I'am getting this error from my js console:
Uncaught ReferenceError: period_one is not defined at HTMLTableCellElement.onblur (gradesTry:1)
'period_one' is the value of one of the select element and also a term/ table
Here is how my complete script files looks:
<script>
function saveToDatabase(editableObj,term,column,id) {
$.ajax({
url: "saveedit.php",
method: "POST",
data:'&editval='+editableObj.innerHTML+'&term='+term+'&column='+column+'&id='+id,
dataType:"text",
success: function(data){
$('#result').html(data);
}
});
}
$(document).ready(function() {
$('#term').on('change', function() {
var term = $('#term').val();
if (term != '') {
$.ajax({
url:"findGrades.php",
method:"post",
data:{"term":term},
dataType:"text",
success:function(data){
$("#result").html(data);
//after the result have been displayed the datatable function is called
$('#dataTable').DataTable();
}
});
} else {
$("#result").html('');
}
});
});
</script>
Contents of saveedit.php
$column = $_POST["column"];
$value = $_POST['editval'];
$id = $_POST['id'];
$term = $_POST['term'];
$query = "UPDATE $term SET $column = '{$value}' WHERE id = $id";
$result = mysqli_query($connection, $query);
if ($result && mysqli_affected_rows($connection) >= 0){
echo "updated successfully";
} else {
die("Database query failed. ". mysqli_error($connection));
}
I'm very sure that I'm not doing things the right way. Will appreciate if you can point me in the right direction on how to do things correctly. Thanks.