0

My question is not a duplicate of 'using HTML5, how do I use contenteditable fields in a form submission'. While the answers suggest I add an id to the div, they fail to explain how I can then insert the content into a database once I've added an id.

I have a div which uses contenteditable to allow the user to edit the text inside it. They're also able to make the text bold by highlighting the desired text and clicking a button.

When attempting to insert the content of the div into a database, I'm receiving the error:

Column 'post' cannot be null

I've gathered that this is because a div cannot have a name. My question is: can I insert the content of the div into a database?

<?php
if(isset($_POST['submit'])) {
$post = $_POST['post'];
$stmt = $con->prepare("INSERT INTO posts (post) VALUES (:post)");
$stmt->bindParam(':post', $post);
$stmt->execute();
print_r($stmt->errorInfo());
}
?>

<form method="post">
<button type="button" id="jBold"><b>B</b></button> // Makes the selected text bold
<div name="post" contenteditable></div>
<input type="submit" name="submit">
</form>

<script>
$(document).ready(function() {
   $('#jBold').click(function() {
     document.execCommand('bold');
   });
});
</script>
Community
  • 1
  • 1
The Codesee
  • 3,714
  • 5
  • 38
  • 78
  • You have to pass that `
    ` value through `ajax`.
    – Nana Partykar May 18 '16 at 16:57
  • I don't believe it's right to mark this post as a duplicate. The post where it supposedly has an answer has some relevance to my question, but does not show how to insert the content of the div into a database and I believe the answers on it are poorly explained. – The Codesee May 18 '16 at 17:00
  • @TheCodesee actually you already have the code to insert the content into the database. So the duplicate question shows you how to submit the content of the div in the form. – Cave Johnson May 18 '16 at 17:03
  • @Andrew I understand that I need to add an `id` to the div but HOW do I insert the contents into the database once I've done that? Do I keep the line of code `$post = $_POST['post'];`? I feel I need a better answer than the one it's supposedly a duplicate of. – The Codesee May 18 '16 at 17:05
  • You are so close. You just need to change it to `$post = $_POST['hiddenTextarea']` and add one more input to your form that looks like `` and change your form to look like `
    ` Copy the javascript function from the duplicate answer as-is and it should work.
    – Cave Johnson May 18 '16 at 17:07

0 Answers0