• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1578
  • Last Modified:

tinyMCE PHP old content after save

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
socom1985
Asked:
socom1985
  • 2
  • 2
1 Solution
 
bupperCommented:
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
 
socom1985Author Commented:
could you please be more specific? I'm setting the fields after the page loads as far as I'm concerned..
0
 
bupperCommented:
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
 
socom1985Author Commented:
Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now