Does it HAVE to be jquery? It can be done with simple ajax, it's much faster and cleaner this way.
The ajax script:
Code:
<script type="text/javascript">
function chgVals(obj, name) {
var url = "exechgs.php"; // name of php file to execute changes
var params = "field=" + name + "&value=" + obj.getElementsByTagName(0).value + '"&id=' + obj.id;
http.open("POST", url + "?" + params, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.send(params);
obj.innerHTML = obj.value;
}
function chgType(obj, name) {
var originalHTML = obj.innerHTML;
obj.innerHTML = '<input type="text" name="' + name + '" value="' + originalHTML + '">';
}
</script>
The form, which needs to have the fields populated from the db.
PHP Code:
<?php
$q = mysql_query("SELECT * FROM user_db") or die(mysql_error());
while($r = mysql_fetch_array( $q )) {
echo <<<EOF
<table>
<tr>
<td>Name:</td>
<td><span id="field$r[id]" onmouseover="chgType(this, 'fullName')" onblur="chgVals(this, 'fullName')">$r[name]</span></td>
</tr>
<tr>
<td>Email:</td>
<td><span id="field$r[id]" onmouseover="chgType(this, 'email')" onblur="chgVals(this, 'email')">$r[email]</span></td>
</tr>
</table>
EOF;
}
?>
The php to execute:
PHP Code:
<?php
$sql = "UPDATE `user_db` SET $_GET[field]='$_GET[value]' WHERE id='$_GET[id]'";
mysql_query($sql);
?>
Please be aware, for purposes of this script, I automatically named the db "user_db" but you need to change that to the ACTUAL name of your existing db.
Yes, I wrote all that for you. You're welcome
Bookmarks