user3320649
user3320649

Reputation: 21

Trying to save the contents of a contenteditable div to MySQL database

Alright, so I want to be able to submit the contents of a contenteditable div into a MySQL database. I've got a (contenteditable='true') div populated with HTML code from a MySQL database. It's something like this:

$page = $_GET['page'];

$getpage = mysql_query("SELECT * FROM pages WHERE title = '$page'") or die(mysql_query());
$row = mysql_fetch_assoc($getpage);
$pagecode = $row['pagecode'];

<div class="editable" contenteditable="true">
<?php echo $pagecode; ?>
</div>

And I want to control this with a regular link:

<a href="#" onclick="somefunction()">Save</a>

Maybe this could work with a hidden form that's controlled with Javascript, but I really have no clue.

The goal is to have something like an HTML editor, which displays the HTML code from the database inside the editable div and allows the user to edit it and save it back in the database. Any help is greatly appreciated.

Thanks,

Gill.

Upvotes: 2

Views: 3853

Answers (2)

Quixrick
Quixrick

Reputation: 3200

In your AJAX code, make a new AJAX request to a processor. Assign an id='id_name_here to your div and pass the id_name_here.innerHTML to the url. Then you can do something with it from your processor.

// JS
function somefunction() {
    var updated_div_info = document.getElementById('id_name_here').innerHTML;
    // POST TO AJAX ...
}


// PHP
<?php

$updated_div_info = $_POST['updated_div_info'];

// INSERT THIS INTO THE DB

Upvotes: 4

Paulo Lima
Paulo Lima

Reputation: 1238

try this

function somefunction(){
    var html = encodeURIComponent($(".editable").html());
    $.ajax({
       url: "save.php",
       data: "content="+html
    })
}

Upvotes: 0

Related Questions