Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

tinyMCE PHP old content after save

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

829 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