Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

tinyMCE PHP old content after save

Posted on 2010-09-14
4
Medium Priority
?
1,561 Views
Last Modified: 2013-12-13
I have a problem with tinyMCE on my page. Everything works fine saving to db and so on. But when I submit the changes tinyMCE refreshes and then I have the old content in the textbox. In the db is the new text..
Can't find the problem.
<?php

include "db.inc.php";
require_once('content/update.class.php');

$saved = false;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Editor</title>

<!-- TinyMCE -->
<script type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
	tinyMCE.init({
		// General options
		mode : "textareas",
		theme : "advanced",
		plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave",

		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,

		// Example content CSS (should be your site CSS)
		content_css : "css/content.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",

		// Style formats
		style_formats : [
			{title : 'Bold text', inline : 'b'},
			{title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
			{title : 'Red header', block : 'h1', styles : {color : '#ff0000'}},
			{title : 'Example 1', inline : 'span', classes : 'example1'},
			{title : 'Example 2', inline : 'span', classes : 'example2'},
			{title : 'Table styles'},
			{title : 'Table row 1', selector : 'tr', classes : 'tablerow1'}
		],

		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}
	});
</script>
<!-- /TinyMCE -->

</head>
<body>
<?php

/* Inhalte abfragen */


$contentnumb = substr($_GET['inh_id'], 0, -1);
$numb = substr($_GET['inh_id'], -1);


/*Ausgabe des Editors*/

echo '<form method="post" action='.$_SERVER['PHP_SELF'].'?inh_id='.$contentnumb.'>';

echo '<div>';
echo '<h3>Editor</h3>';

echo '<div>';
echo '<textarea id="elm1" name="elm1" rows="10" cols="80" style="width: 80%">';

$query = "SELECT * FROM content where cnt_id=".$contentnumb;
$inhalt = mysql_query($query);
$row = mysql_fetch_assoc($inhalt);

switch ($numb) {
    case 1:
        $content = mysql_result($inhalt, 0, 'cnt_textbox1');
        break;
    case 2:
        $content = mysql_result($inhalt, 0, 'cnt_textbox2');
        break;
    case 3:
        $content = mysql_result($inhalt, 0, 'cnt_textbox3');
        break;
}

echo $content;

?>
		</textarea>
		</div>

		<!-- Some integration calls -->
		<a href="javascript:;" onmousedown="tinyMCE.get('elm1').show();">[Show]</a>
		<a href="javascript:;" onmousedown="tinyMCE.get('elm1').hide();">[Hide]</a>
		<a href="javascript:;" onmousedown="tinyMCE.get('elm1').execCommand('Bold');">[Bold]</a>
		<a href="javascript:;" onmousedown="alert(tinyMCE.get('elm1').getContent());">[Get contents]</a>
		<a href="javascript:;" onmousedown="alert(tinyMCE.get('elm1').selection.getContent());">[Get selected HTML]</a>
		<a href="javascript:;" onmousedown="alert(tinyMCE.get('elm1').selection.getContent({format : 'text'}));">[Get selected text]</a>
		<a href="javascript:;" onmousedown="alert(tinyMCE.get('elm1').selection.getNode().nodeName);">[Get selected element]</a>
		<a href="javascript:;" onmousedown="tinyMCE.execCommand('mceInsertContent',false,'<b>Hello world!!</b>');">[Insert HTML]</a>
		<a href="javascript:;" onmousedown="tinyMCE.execCommand('mceReplaceContent',false,'<b>{$selection}</b>');">[Replace selection]</a>

		<br />
		<input type="submit" name="save" value="Submit" />
		<input type="reset" name="reset" value="Reset" />
	</div>
</form>

<?php


/*Eingabe speichern*/

if (isset($_SESSION['log_name']))
{
if (isset($_POST['save']))
{

$contentnumb = substr($_GET['inh_id'], 0, -1);
$numb = substr($_GET['inh_id'], -1);

$cnt_textbox = $_POST['elm1'];

$update = new update();

$update->saveContent($contentnumb,$numb,$cnt_textbox);

$saved = true;


}
}
?>



</body>
</html>

Open in new window

0
Comment
Question by:socom1985
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:bupper
ID: 33677428
A browser REFRESH will, in most browsers, preserve the text in INPUT, TEXTAREA, and other editable fields.

So to do it properly, you would use javascript to set the values of those fields AFTER the page has loaded. That way, you can refresh the page and set it the the proper new values.
0
 
LVL 1

Author Comment

by:socom1985
ID: 33679812
could you please be more specific? I'm setting the fields after the page loads as far as I'm concerned..
0
 
LVL 5

Accepted Solution

by:
bupper earned 2000 total points
ID: 33683999
You have a little php script that outputs the contents to the textarea.

Instead, output the contents to a javascript variable. Then, set the contents of the textarea using javascript.

Attached is the code with this done (copy and paste, it should run properly). REPLACE from the start of the textarea (echo) to the end of the textarea element (</textarea>). This is NOT TESTED so if it doesn't work properly, there might be a little issue in the code :)
echo '<textarea id="elm1" name="elm1" rows="10" cols="80" style="width: 80%">';

$query = "SELECT * FROM content where cnt_id=".$contentnumb;
$inhalt = mysql_query($query);
$row = mysql_fetch_assoc($inhalt);

switch ($numb) {
    case 1:
        $content = mysql_result($inhalt, 0, 'cnt_textbox1');
        break;
    case 2:
        $content = mysql_result($inhalt, 0, 'cnt_textbox2');
        break;
    case 3:
        $content = mysql_result($inhalt, 0, 'cnt_textbox3');
        break;
}
?>
		</textarea>
<script type="text/javascript">
var textarea_content = "<?php echo str_replace('"', '\\"', $content); ?>";
document.getElementById('elm1').innerHTML = textarea_content;
</script>

Open in new window

0
 
LVL 1

Author Closing Comment

by:socom1985
ID: 33690828
Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question