Click here to Skip to main content
15,038,451 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:
<script src=""></script>
function showEdit(editableObj) 
function saveToDB(editableObj,column,id) 
  url: "page2.php",
  type: "POST",
  success: function(data)

The column of my table is as under:
<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:
include_once 'includes/db_connect.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.
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