Tämä on opas WYSIWYG-editorin tekemiseen PHP:lla.
1. Lataa Kevin Rothin Cross-Browser Rich Text Editor osoitteesta: http://www.kevinroth.com/rte/demo.htm
2. Tee alla olevan listauksen 1 mukaiset muutokset demo.php tiedostoon ja uudelleen nimeä tiedosto index.php:ksi.
3. Tee submit.php ja lisää siihen alla oleva listaus 2.
4. Tee content.txt tiedosto.
5. Lataa tiedostot palvelimelle haluamaasi kansioon ja muuta content.txt tiedoston oikeudet 777 CHMOD-toiminnolla. Huom! On parasta tehdä ihan oma kansio jossa tämä editori on. Esim. htmledit.
6. Sisällytä content.txt johonkin php tiedostoon (include ("htmledit/content.txt"))..
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <?php $filename_palaute = "content.txt" ; $file_palaute = file($filename_palaute); $file_palaute = array_unique($file_palaute); $fd_palaute = fopen ($filename_palaute , "r"); $fstring_palaute = fread ($fd_palaute , filesize ($filename_palaute)) ; ?> <html> <head> <title>Cross-Browser Rich Text Editor</title> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <meta name="PageURL" content="http://www.kevinroth.com/rte/demo.php" /> <meta name="PageTitle" content="Cross-Browser Rich Text Editor (PHP Demo)" /> <script language="JavaScript" type="text/javascript" src="html2xhtml.js"></script> <!-- To decrease bandwidth, use richtext_compressed.js instead of richtext.js //--> <script language="JavaScript" type="text/javascript" src="richtext.js"></script> </head> <body> <!-- START Demo Code --> <form name="RTEDemo" action="submit.php" method="post" onsubmit="return submitForm();"> <!-- <?=$_SERVER["PHP_SELF"]?> --> <script language="JavaScript" type="text/javascript"> <!-- function submitForm() { //make sure hidden and iframe values are in sync before submitting form //to sync only 1 rte, use updateRTE(rte) //to sync all rtes, use updateRTEs updateRTE('rte1'); //updateRTEs(); //change the following line to true to submit form return true; } //Usage: initRTE(imagesPath, includesPath, cssFile, genXHTML) initRTE("images/", "", "", true); //--> </script> <noscript><p><b>Javascript must be enabled to use this form.</b></p></noscript> <script language="JavaScript" type="text/javascript"> <!-- <?php //format content for preloading if (!(isset($_POST["rte1"]))) { $content = "$fstring_palaute " . chr(13); $content = rteSafe($content); } else { //retrieve posted value $content = rteSafe($_POST["rte1"]); } ?>//Usage: writeRichText(fieldname, html, width, height, buttons, readOnly) writeRichText('rte1', '<?=$content;?>', 520, 200, true, false); //--> </script> <p>Click submit to post the form and reload with your rte content.</p> <p><input type="submit" name="submit" value="Submit"></p> </form> <?php function rteSafe($strText) { //returns safe code for preloading in the RTE $tmpString = $strText; //convert all types of single quotes $tmpString = str_replace(chr(145), chr(39), $tmpString); $tmpString = str_replace(chr(146), chr(39), $tmpString); $tmpString = str_replace("'", "'", $tmpString); //convert all types of double quotes $tmpString = str_replace(chr(147), chr(34), $tmpString); $tmpString = str_replace(chr(148), chr(34), $tmpString); // $tmpString = str_replace("\"", "\"", $tmpString); //replace carriage returns & line feeds $tmpString = str_replace(chr(10), " ", $tmpString); $tmpString = str_replace(chr(13), " ", $tmpString); return $tmpString; } fclose($fd_palaute) ; ?> <!-- END Demo Code --> </body> </html>
<?php $content = $_POST[rte1]; $pakolliset = @explode(",", $_POST[pakolliset]); $luku = count($pakolliset)-1; for($n=0;$n<=$luku;$n++) { if($pakolliset[$n] != "") { $x = $pakolliset[$n]; if($_POST[$x] == "") { $stop = "yes"; } } } if($stop == "") { foreach($_POST as $x => $y) { if( ($x == "pakolliset") || ($x == "kuittaus")) { continue; } $viesti .= "$x: $y\n"; } $filename_palaute = "content.txt" ; $fd_palaute = fopen ($filename_palaute , "w"); $fcounted_palaute = $fstring_palaute.$content; $fout_palaute = fwrite ($fd_palaute , $fcounted_palaute ); fclose($fd_palaute); if($_POST[kuittaus] != "") {header("location: $_POST[kuittaus]");} else { print("<b>Sisältö on nyt tallennettu! <a href=\"index.php\">Palaa editoriin!</a></b>"); } } else { print("<b>Ole hyvä ja täytä kenttä!</b>"); } ?>
Aihe on jo aika vanha, joten et voi enää vastata siihen.