Click here to Skip to main content
15,887,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a list from the database and I want to implement edit functionality where in onclicking a table column, the column becomes editable and on clicking out of column, the value gets updated.
I have used AJAX for this purpose. My code is as under:
Page1.php
JavaScript
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
function showEdit(editableObj) 
{
 $(editableObj).css("background","#FFF");
} 
function saveToDB(editableObj,column,id) 
{
 $.ajax(
 {
  url: "page2.php",
  type: "POST",
  data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
  success: function(data)
  {
   $(editableObj).css("background","#FDFDFD");
  }        
 });
}
</script>

The column of my table is as under:
PHP
<td contenteditable="true" onBlur="saveToDB(this, 'exmid','<?php echo $p0; ?>')"
         onClick="showEdit(this);"><?php echo $p3 ?>

Note: $p0 contains the serial no of row from mysql database table and $p3 contains the displayed text.
The code for page2.php is:
PHP
<?php
include_once 'includes/db_connect.php';
?>
<?php
$result = mysql_query("UPDATE examlist1 set " . $_POST["column"] . " =     '".$_POST["editval"]."' WHERE  sno=".$_POST["id"]);
?>

Problem: When i click on the column it becomes editable. Using alert() inside saveToDB() This is the first time I am trying to use ajax in a php code myself.
Plz suggest what is the problem and what is the solution. The code is being implemented on a Linux based server hosted at Godaddy using PHP 5.4
Also I would like to set the background color on fail. How to write it inside ajax block?

What I have tried:

I have checked that the function is called on clicking out of the column and also values of column and id are correct.
Then i tried the alert() function inside $.ajax and it was not called. I am not sure whether ajax is running or not.
Posted
Comments
Member 8057273 15-Dec-16 0:00am    
Hi. I always use parameterized quey but here, column name was supposed to pass dynamically. I mean, before '=', can a parameter be passed?
Richard Deeming 15-Dec-16 8:07am    
No, you can't use a parameter for a table or column name.

But you shouldn't blindly accept whatever value the user sends to your page either. You should have a list of valid column names, and verify that the column name exists within that list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900