Oli,
I am absolutely blown away by tabulator!
I've figured out how to allow user edits to be passed back to MySQL as individual cell updates. My question will be how to efficiently adapt my setup such that any cell in the row will update correctly without having to hard code each field in my code.
The example I have working properly is as such:
AJAX (PHP):
$comments=$_POST['comments'];
$theID=$_POST['theID'];
$sql= mysqli_query($conn,"UPDATE test SET comments = '".$comments."' WHERE id = '".$theID."'");
TABLE CONSTRUCT:
columns:[
{title:"Project", field:"proj_id"},
{title: "Comments", field:"comments", width: 500, editable:true, editor: "input", visible:true},
]
CALLBACK:
cellEdited:function(cell){
//this is called whenever a cell's value is edited.
var comments= cell.getValue();
var theID = cell.getRow().getIndex();
console.log(comments);
console.log(theID);
$.ajax({
url: "ajax.php",
data: "comments=" + comments+ "&theID=" + theID,
type: "POST",
error: function(XMLHttpRequest, textStatus, error){
alert("AJAX error: " + textStatus + "; " + error);
}
})
My use case will have multiple users potentially updating different cells in a row so I am trying to avoid crashing..
Thank you in advance!
I should note that I am passing pk_id in as id to my table as well.
Update your callback to include the field of the cell being edited and add this to the data being sent via the AJAX request e.g.
cellEdited:function(cell){
//this is called whenever a cell's value is edited.
var value = cell.getValue();
var theID = cell.getRow().getIndex();
var field = cell.getField();
console.log(comments);
console.log(theID);
$.ajax({
url: "ajax.php",
data: "value=" + value + "&field=" + field + "&theID=" + theID,
type: "POST",
error: function(XMLHttpRequest, textStatus, error){
alert("AJAX error: " + textStatus + "; " + error);
}
})
Then update your PHP to include the field (plus ID and value) e.g.
$value=$_POST['value'];
$theID=$_POST['theID'];
$field=$_POST['field']
$sql= mysqli_query($conn,"UPDATE test SET '".$field."' = '".$value."' WHERE id = '".$theID."'");
Hope this helps
If I manually call out the column ("field") in my sql statement I can get $value and $theID to operate correctly in my ajax.php. I have tried for hours now to figure out how to get $field working correctly but I have to cave in and ask for more help :(
I have tried the exact code you provided as well as adding .toString() behind the cell.getValue() statement as cell.getValue().toString() and that doesn't seem to work either.
I have exhausted all methods I know to troubleshoot the issue. Any other ideas?
I'm realizing the message above may have been confusing so here is the actual code again:
'''
cellEdited:function(cell){
//this is called whenever a cell's value is edited.
var value = cell.getValue();
var field = cell.getField();
var theID = cell.getRow().getIndex();
console.log(value);
console.log(field);
console.log(theID);
$.ajax({
url: "ajax.php",
data: "value=" + value + "&field=" + field + "&theID=" + theID,
type: "POST",
error: function(XMLHttpRequest, textStatus, error){
alert("AJAX error: " + textStatus + "; " + error);
}
})
}
$field=$_POST['field'];
$value=$_POST['value'];
$theID=$_POST['theID'];
$sql= mysqli_query($conn,"UPDATE test SET '".$field."' = '".$value."' WHERE id = '".$theID."'");
'''
On closer inspection I think the problem is that you don't need all the additional "" and .. in your sql query as you are not passing an array to $_POST
Try adjusting your sql query to:
$sql= mysqli_query($conn, "UPDATE test SET $field = '$value' WHERE id = '$theID'");
Thank you tomheaps! That seems to have been the issue. I really appreciate the help.
No problems. I am relatively new to PHP as well and understand how frustrating it can be trying to achieve the simplest of things - relatively few resources on the web as well compared with other languages to look stuff up!
100% agree. I feel like I know what has to be done but its the little details that kill all of my time. This was a big hurdle for my project though so again I really appreciate the help.