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>