So I have put together a custom WYSIWYG editor for posting out. Here is the JS file:
function iframe(){
editor.document.designMode = 'on';
}
function bold(){
editor.document.execCommand('bold', false, null);
}
function italic(){
editor.document.execCommand('italic', false, null);
}
function underline(){
editor.document.execCommand('underline', false, null);
}
function fontsize(){
var size = prompt("Enter a size (1-7)", "");
editor.document.execCommand('fontsize', false, size);
}
function fontcolor(){
var color = prompt("Enter a hex code or name of color", "");
editor.document.execCommand('forecolor', false, color);
}
function highlight(){
editor.document.execCommand('backcolor', false, "yellow");
}
function link(){
var link = prompt("Enter a link", "http://");
editor.document.execCommand('createLink', false, link);
}
function unlink(){
editor.document.execCommand('unlink', false, null);
}
function formsubmit(){
document.getElementById("textarea").value = window.frames['editor'].document.body.innerHTML;
document.getElementById("rtf").submit();
}
Here is the form it latches onto:
<form action="actions/newDocAdd.php" method="post" id="rtf">
<input type="text" name="doc_title" id="doc_title" required="required" placeholder="Document Title"/><br />
<input type="button" value="B" onclick="bold()">
<input type="button" value="I" onclick="italic()">
<input type="button" value="U" onclick="underline()">
<input type="button" value="Size" onclick="fontsize()">
<input type="button" value="Color" onclick="fontcolor()">
<input type="button" value="Highlight" onclick="highlight()">
<input type="button" value="Link" onclick="link()">
<input type="button" value="Unlink" onclick="unlink()">
<br><br>
<textarea name="doc_content" id="doc_content" placeholder="Document Content" style="display: none;"></textarea>
<iframe name="editor" id="editor" style="width:100%; height: 500px;"></iframe>
<br><br>
<input onclick="formsubmit()" type="submit" value="Create Document" name="submit"/><br />
</form>
And here is the php script that does the work:
<?php=
if(isset($_POST["submit"])){
$hostname='localhost';
$username='******';
$password='******';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=******",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "INSERT INTO doc_list (doc_title, doc_content, doc_created) VALUES ('".$_POST["doc_title"]."','".$_POST["doc_content"]."', NOW() )";
if ($dbh->query($sql)) {
header ('Location: ../docList.php');
}
else{
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
?>
What is happening is that while creating the post all the JS editor bits work but when it posts the content out it fails to update the database now (where as it did before I made these changes from a simple input field to the editable textarea/iframe. Maybe I am missing something very simple here but cannot spot why it fails to post into the DB any longer?