Forum


15:57

04/11/2008

Example code to save the results of editRow to a database using PHP.
Add this to your jqGrid parameters:
editurl: 'example.php',
Here's a function to enable the row for editing when it is clicked on (see examples in jqGrid manual for editRow method):
if(id && id!==lastSel){
jQuery('#list2').restoreRow(lastSel);
jQuery('#list2').editRow(id, true,on_edit,checksave);
lastSel=id;
}
}
Here are the two functions required by editRow() because keys is true. checksave() is the succesfunc and on_edit() is the oneditfunc.
// check to see if POST returns a good HTTP response
if (result.status != 200) {
alert(result.statusText);
alert(”Update failed!”);
return false;
}
// the PHP code sends OK as the first 2 characters if it did the db update successfully
// if the response does not begin with 'OK'
// display whatever is returned (probably error msgs)
if (result.responseText.substring(0,2) != “OK”) {
alert(result.responseText);
return false;
}
return true;
}
function on_edit(result) {
// do nothing
return true;
}
Here is the code for example.php:
// database connection parameters
$db='';
$host='';
$user='';
$pass='';
// connect to the MySQL database server
$dbh = mysql_connect($host,$user,$pass) or die(”Connection Error: ” . mysql_error());
// select the database
mysql_select_db($db) or die(”Error selecting '$db' database.”);
// get clean id
$id=mysql_real_escape_string($_POST['id']);
$fields = '';
$values = '';
// $exceptions - fields that will not be UPDATEd into table
// i.e. 'submit, action, '; (note trailing comma and space!)
$exceptions='submit, id, ';
// format input POST fields into SQL syntax for the list of fields and values after SET
foreach ($_POST as $field => $value) {
if (!preg_match(”/$field, /”, $exceptions)) {
$value = mysql_real_escape_string($value);
$fields .= “$field = '$value', “;
}
}
// remove trailing “, ” from $fields
$fields = preg_replace('/, $/', '', $fields);
$query = “UPDATE tmpsale SET $fields WHERE id='$id'”;
//echo $query; // for debugging, any output text that does not begin with “OK” will display in an alert box
$result = mysql_query($query);
if (!$result) {
die('Query error:' . mysql_error());
}
if (mysql_affected_rows($dbh) > 0) {
echo “OK\\n”;
} else {
echo “Update Failed.\\n”;
}
?>
Most Users Ever Online: 715
Currently Online:
55 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
OlegK: 1255
markw65: 179
kobruleht: 144
phicarre: 132
YamilBracho: 124
Renso: 118
Member Stats:
Guest Posters: 447
Members: 11373
Moderators: 2
Admins: 1
Forum Stats:
Groups: 1
Forums: 8
Topics: 10592
Posts: 31289
Newest Members:
, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.comModerators: tony: 7721, Rumen[Trirand]: 81
Administrators: admin: 66