Skip to content

Instantly share code, notes, and snippets.

@nicksheffield
Created March 8, 2011 04:35
Show Gist options
  • Save nicksheffield/859848 to your computer and use it in GitHub Desktop.
Save nicksheffield/859848 to your computer and use it in GitHub Desktop.
Edit in place using ajax and contenteditable
<?php
// quit script if you aren't accessing it with ajax
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) == false){
die();
}
// include database class
include('database.class.php');
// initialize database class
$db = new database('localhost','user','pass','db');
// send the data to the database
$query = $db->query("UPDATE table SET content='".$_POST['content']."' WHERE id='".$_POST['id']."'";
// if successful, echo true, else false
echo !!$query;
<section id="1">
<p contenteditable="true">
Some text here
</p>
</section>
$('p[contenteditable=true]').live('blur',function(){
$.ajax({
type:'POST',
url:'edit_item.php',
data:{
content: $(this).text(),
id: $(this).parent().attr('id')
},
success:function(msg){
if(!msg){
//console.error('update failure');
alert('update failure');
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment