Solved

tinyMCE PHP old content after save

Posted on 2010-09-14
4
1,473 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 500 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article discusses four methods for overlaying images in a container on a web page
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

815 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now