Hi there!
I'm trying to use CKEditor 3 in php with the jquery forms plugin and then saving it to an xml-file.
The problem I'm having is that I have to hit the submit button twice for changes to be saved.
My code:
In the php-page that contains CKEditor:
<head> <title>CMS</title> <meta content="text/html; charset=utf-8" http-equiv="content-type"/> <meta name="robots" content="noindex, nofollow"/> <link href="css/styles.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery-1.4.2.min.js"></script> <script type="text/javascript" src="jquery.form.js"></script> <script type="text/javascript"> // prepare the form when the DOM is ready $(document).ready(function() { // bind form using ajaxForm $('#myForm').ajaxForm ({ // target identifies element that contains txt that the xml-file is saved target: '#message', // success identifies the function to invoke when the server response // has been received; here we apply a fade-in & delayed fade-out effect to the new content success: function() { //fadeIn txt xml is saved $('#message').fadeIn('fast'); //fadeOut txt xml is saved $('#message').delay(2500).fadeOut('slow'); } }); }); </script> </head> <body> <h1> CKEditor Sample </h1> <!-- <div> contains alert message. --> <div id="alerts"> <noscript> <p> <strong>CMS needsJavaScript</strong>. </p> </noscript> </div> <form id="myForm" action="sparaxml.php" method="post"> <p> <?php // Include CKEditor php. include_once "ckeditor.php"; //Load XML $xml = simplexml_load_file('xml/home.xml'); //Conent $initialValue = $xml; // Class instance. $CKEditor = new CKEditor(); // Path to ckeditor.js: $CKEditor->basePath = ''; // configure CKEditor $CKEditor->Config['customConfig'] = 'config.js'; // Create textarea element och attach CKEditor. $CKEditor->editor("contentHome", $initialValue); ?> <input type="submit" value="Save"/> </p> </form> <div id="message"></div> </body>
The php-file that saves to xml-file:
$xmlData = $_POST["contentHome"]; $content = "<?xml version='1.0' encoding='utf-8'?>\r\n<text><![CDATA[" . $xmlData . "]]></text>"; $filename = 'xml/home.xml'; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // We're opening $filename in writable mode. if (!$handle = fopen($filename, 'w')) { echo '<div style="background-color:#E0E0E0; padding:20px"><p>FEL:Kan inte öppna filen ($filename)</p></div>'; exit; } // Write $content to our opened file. if (fwrite($handle, $content) === FALSE) { echo '<div style="background-color:#E0E0E0; padding:20px"><p>FEL:Kan inte skriva till filen.</p></div>'; exit; } echo '<div style="background-color:#F5EFE5; padding:20px"><p>Uppdateringen är sparad.</p></div>'; fclose($handle); } else { echo '<div style="background-color:#E0E0E0; padding:20px"><p>FEL:Filen är inte skrivbar.</p></div>'; }
If I don't use jquery form it saves correct every time.
What am I doing wrong here? Help...
Niklas