Skip to content

Instantly share code, notes, and snippets.

@dopsmain
Forked from nicksheffield/edit_item.php
Created April 19, 2017 13:58
Show Gist options
  • Save dopsmain/f141f019db427a1a40f55b497cbfc19d to your computer and use it in GitHub Desktop.
Save dopsmain/f141f019db427a1a40f55b497cbfc19d 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