Foros del Web » Programando para Internet » Javascript »

Pasar texto de editor iframe RTF a PHP

Estas en el tema de Pasar texto de editor iframe RTF a PHP en el foro de Javascript en Foros del Web. Hola. Había pensado en adaptar este editor RFT a mi blog. Pero debido a mi desconocimiento en JavaScrip no sé como pasar el texto que ...
  #1 (permalink)  
Antiguo 12/02/2012, 06:44
 
Fecha de Ingreso: abril-2011
Ubicación: España, Mdrid
Mensajes: 89
Antigüedad: 13 años
Puntos: 1
Pasar texto de editor iframe RTF a PHP

Hola. Había pensado en adaptar este editor RFT a mi blog. Pero debido a mi desconocimiento en JavaScrip no sé como pasar el texto que escribo a una variable GET en PHP.

Código Javascript:
Ver original
  1. <input type="submit" name="archivo" id="archivo" value="Guardar" onclick="var nombre=prompt('Introduce el nombre del archivo:');document.location.href='guardar.php?archivo='+nombre+'&contenido='/*¿Qué tendría que poner aquí?*/;">
Evidentemente sería recuperar el texto en formato HTML.

Gracias de antemano.

Última edición por GuillermoM; 12/02/2012 a las 07:39
  #2 (permalink)  
Antiguo 12/02/2012, 16:32
 
Fecha de Ingreso: abril-2011
Ubicación: España, Mdrid
Mensajes: 89
Antigüedad: 13 años
Puntos: 1
Respuesta: Pasar texto de editor iframe RTF a PHP

Seguramente no me he sabido explicar bien:


Código Javascript:
Ver original
  1. <html>
  2. <head>
  3. <title>Mozilla Rich Text Editing Demo</title>
  4.  
  5. <style type="text/css">
  6. .imagebutton {height: 22; width: 23; border: solid 2px #C0C0C0; background-color: #C0C0C0}
  7. .image {position: relative; left: 1; top: 1; height:20; width:21; border:none;}
  8. .toolbar {height: 30; background-color: #C0C0C0;}
  9. </style>
  10.  
  11. <script>
  12.  
  13. var command = "";
  14.  
  15. function InitToolbarButtons() {
  16.   var kids = document.getElementsByTagName('DIV');
  17.  
  18.   for (var i=0; i < kids.length; i++) {
  19.     if (kids[i].className == "imagebutton") {
  20.       kids[i].onmouseover = tbmouseover;
  21.       kids[i].onmouseout = tbmouseout;
  22.       kids[i].onmousedown = tbmousedown;
  23.       kids[i].onmouseup = tbmouseup;
  24.       kids[i].onclick = tbclick;
  25.     }
  26.   }
  27. }
  28.  
  29. function tbmousedown(e)
  30. {
  31.   var evt = e ? e : window.event;
  32.  
  33.   this.firstChild.style.left = 2;
  34.   this.firstChild.style.top = 2;
  35.   this.style.border="inset 2px";
  36.   if (evt.returnValue) {
  37.     evt.returnValue = false;
  38.   } else if (evt.preventDefault) {
  39.     evt.preventDefault( );
  40.   } else {
  41.     return false;
  42.   }
  43. }
  44.  
  45. function tbmouseup()
  46. {
  47.   this.firstChild.style.left = 1;
  48.   this.firstChild.style.top = 1;
  49.   this.style.border="outset 2px";
  50. }
  51.  
  52. function tbmouseout()
  53. {
  54.   this.style.border="solid 2px #C0C0C0";
  55. }
  56.  
  57. function tbmouseover()
  58. {
  59.   this.style.border="outset 2px";
  60. }
  61.  
  62.   function insertNodeAtSelection(win, insertNode)
  63.   {
  64.       // get current selection
  65.       var sel = win.getSelection();
  66.  
  67.       // get the first range of the selection
  68.       // (there's almost always only one range)
  69.       var range = sel.getRangeAt(0);
  70.  
  71.       // deselect everything
  72.       sel.removeAllRanges();
  73.  
  74.       // remove content of current selection from document
  75.       range.deleteContents();
  76.  
  77.       // get location of current selection
  78.       var container = range.startContainer;
  79.       var pos = range.startOffset;
  80.  
  81.       // make a new range for the new selection
  82.       range=document.createRange();
  83.  
  84.       if (container.nodeType==3 && insertNode.nodeType==3) {
  85.  
  86.         // if we insert text in a textnode, do optimized insertion
  87.         container.insertData(pos, insertNode.nodeValue);
  88.  
  89.         // put cursor after inserted text
  90.         range.setEnd(container, pos+insertNode.length);
  91.         range.setStart(container, pos+insertNode.length);
  92.  
  93.       } else {
  94.  
  95.  
  96.         var afterNode;
  97.         if (container.nodeType==3) {
  98.  
  99.           // when inserting into a textnode
  100.           // we create 2 new textnodes
  101.           // and put the insertNode in between
  102.  
  103.           var textNode = container;
  104.           container = textNode.parentNode;
  105.           var text = textNode.nodeValue;
  106.  
  107.           // text before the split
  108.           var textBefore = text.substr(0,pos);
  109.           // text after the split
  110.           var textAfter = text.substr(pos);
  111.  
  112.           var beforeNode = document.createTextNode(textBefore);
  113.           afterNode = document.createTextNode(textAfter);
  114.  
  115.           // insert the 3 new nodes before the old one
  116.           container.insertBefore(afterNode, textNode);
  117.           container.insertBefore(insertNode, afterNode);
  118.           container.insertBefore(beforeNode, insertNode);
  119.  
  120.           // remove the old node
  121.           container.removeChild(textNode);
  122.  
  123.         } else {
  124.  
  125.           // else simply insert the node
  126.           afterNode = container.childNodes[pos];
  127.           container.insertBefore(insertNode, afterNode);
  128.         }
  129.  
  130.         range.setEnd(afterNode, 0);
  131.         range.setStart(afterNode, 0);
  132.       }
  133.  
  134.       sel.addRange(range);
  135.   };
  136.  
  137. function getOffsetTop(elm) {
  138.  
  139.   var mOffsetTop = elm.offsetTop;
  140.   var mOffsetParent = elm.offsetParent;
  141.  
  142.   while(mOffsetParent){
  143.     mOffsetTop += mOffsetParent.offsetTop;
  144.     mOffsetParent = mOffsetParent.offsetParent;
  145.   }
  146.  
  147.   return mOffsetTop;
  148. }
  149.  
  150. function getOffsetLeft(elm) {
  151.  
  152.   var mOffsetLeft = elm.offsetLeft;
  153.   var mOffsetParent = elm.offsetParent;
  154.  
  155.   while(mOffsetParent){
  156.     mOffsetLeft += mOffsetParent.offsetLeft;
  157.     mOffsetParent = mOffsetParent.offsetParent;
  158.   }
  159.  
  160.   return mOffsetLeft;
  161. }
  162.  
  163. function tbclick()
  164. {
  165.   if ((this.id == "forecolor") || (this.id == "hilitecolor")) {
  166.     parent.command = this.id;
  167.     buttonElement = document.getElementById(this.id);
  168.     document.getElementById("colorpalette").style.left = getOffsetLeft(buttonElement);
  169.     document.getElementById("colorpalette").style.top = getOffsetTop(buttonElement) + buttonElement.offsetHeight;
  170.     document.getElementById("colorpalette").style.visibility="visible";
  171.   } else if (this.id == "createlink") {
  172.     var szURL = prompt("Enter a URL:", "http://");
  173.     if ((szURL != null) && (szURL != "")) {
  174.       document.getElementById('edit').contentWindow.document.execCommand("CreateLink",false,szURL);
  175.     }
  176.   } else if (this.id == "createimage") {
  177.     imagePath = prompt('Enter Image URL:', 'http://');
  178.     if ((imagePath != null) && (imagePath != "")) {
  179.       document.getElementById('edit').contentWindow.document.execCommand('InsertImage', false, imagePath);
  180.     }
  181.   } else if (this.id == "createtable") {
  182.     e = document.getElementById("edit");
  183.     rowstext = prompt("enter rows");
  184.     colstext = prompt("enter cols");
  185.     rows = parseInt(rowstext);
  186.     cols = parseInt(colstext);
  187.     if ((rows > 0) && (cols > 0)) {
  188.       table = e.contentWindow.document.createElement("table");
  189.       table.setAttribute("border", "1");
  190.       table.setAttribute("cellpadding", "2");
  191.       table.setAttribute("cellspacing", "2");
  192.       tbody = e.contentWindow.document.createElement("tbody");
  193.       for (var i=0; i < rows; i++) {
  194.         tr =e.contentWindow.document.createElement("tr");
  195.         for (var j=0; j < cols; j++) {
  196.           td =e.contentWindow.document.createElement("td");
  197.           br =e.contentWindow.document.createElement("br");
  198.           td.appendChild(br);
  199.           tr.appendChild(td);
  200.         }
  201.         tbody.appendChild(tr);
  202.       }
  203.       table.appendChild(tbody);      
  204.       insertNodeAtSelection(e.contentWindow, table);
  205.     }
  206.   } else {
  207.     document.getElementById('edit').contentWindow.document.execCommand(this.id, false, null);
  208.   }
  209. }
  210.  
  211. function Select(selectname)
  212. {
  213.   var cursel = document.getElementById(selectname).selectedIndex;
  214.   /* First one is always a label */
  215.   if (cursel != 0) {
  216.     var selected = document.getElementById(selectname).options[cursel].value;
  217.     document.getElementById('edit').contentWindow.document.execCommand(selectname, false, selected);
  218.     document.getElementById(selectname).selectedIndex = 0;
  219.   }
  220.   document.getElementById("edit").contentWindow.focus();
  221. }
  222.  
  223. function dismisscolorpalette()
  224. {
  225.   document.getElementById("colorpalette").style.visibility="hidden";
  226. }
  227.  
  228. function Start() {
  229.   document.getElementById('edit').contentWindow.document.designMode = "on";
  230.   try {
  231.     document.getElementById('edit').contentWindow.document.execCommand("undo", false, null);
  232.   }  catch (e) {
  233.     alert("This demo is not supported on your level of Mozilla.");
  234.   }
  235.  
  236.   InitToolbarButtons();
  237.   if (document.addEventListener) {
  238.     document.addEventListener("mousedown", dismisscolorpalette, true);
  239.     document.getElementById("edit").contentWindow.document.addEventListener("mousedown", dismisscolorpalette, true);
  240.     document.addEventListener("keypress", dismisscolorpalette, true);
  241.     document.getElementById("edit").contentWindow.document.addEventListener("keypress", dismisscolorpalette, true);
  242.   } else if (document.attachEvent) {
  243.     document.attachEvent("mousedown", dismisscolorpalette, true);
  244.     document.getElementById("edit").contentWindow.document.attachEvent("mousedown", dismisscolorpalette, true);
  245.     document.attachEvent("keypress", dismisscolorpalette, true);
  246.     document.getElementById("edit").contentWindow.document.attachEvent("keypress", dismisscolorpalette, true);
  247.   }
  248. }
  249.  
  250. </script>
  251. </head>
  252. <body onLoad="Start()">
  253. <br>
  254. <iframe id="edit" width="100%" height="200px"></iframe>
  255. <iframe width="250" height="170" id="colorpalette" src="colors.html" style="visibility:hidden; position: absolute;"></iframe>
  256.  
  257. <script>
  258. function viewsource(source)
  259. {
  260.   var html;
  261.   if (source) {
  262.     html = document.createTextNode(document.getElementById('edit').contentWindow.document.body.innerHTML);
  263.     document.getElementById('edit').contentWindow.document.body.innerHTML = "";
  264.     html = document.getElementById('edit').contentWindow.document.importNode(html,false);
  265.     document.getElementById('edit').contentWindow.document.body.appendChild(html);
  266.     document.getElementById("toolbar1").style.visibility="hidden";
  267.     document.getElementById("toolbar2").style.visibility="hidden";  
  268.   } else {
  269.     html = document.getElementById('edit').contentWindow.document.body.ownerDocument.createRange();
  270.     html.selectNodeContents(document.getElementById('edit').contentWindow.document.body);
  271.     document.getElementById('edit').contentWindow.document.body.innerHTML = html.toString();
  272.     document.getElementById("toolbar1").style.visibility="visible";
  273.     document.getElementById("toolbar2").style.visibility="visible";  
  274.   }
  275. }
  276.  
  277. function usecss(source)
  278. {
  279.   document.getElementById('edit').contentWindow.document.execCommand("useCSS", false, !(source));  
  280. }
  281.  
  282. function readonly(source)
  283. {
  284.     document.getElementById('edit').contentWindow.document.execCommand("readonly", false, !(source));  
  285. }
  286. </script>
  287. <input type="checkbox" onclick="viewsource(this.checked)">
  288. View HTML Source</input>
  289.  
  290. <input checked type="checkbox" onclick="usecss(this.checked)">
  291. Use CSS</input>
  292. <input type="checkbox" onclick="readonly(this.checked)">
  293. Read only</input>
  294.  
  295. </body>
  296. </html>

Como puedo obtener lo escrito de este código a una variable PHP.
  #3 (permalink)  
Antiguo 12/02/2012, 18:35
Avatar de Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años
Puntos: 834
Respuesta: Pasar texto de editor iframe RTF a PHP

Con esto obtenés el valor:
Código PHP:
var html=document.getElementById('edit').contentWindow.document.body.innerHTML
Luego podrás asignar ese valor como value de un campo oculto o algo por el estilo para enviarlo al servidor.
  #4 (permalink)  
Antiguo 15/02/2012, 10:55
 
Fecha de Ingreso: abril-2011
Ubicación: España, Mdrid
Mensajes: 89
Antigüedad: 13 años
Puntos: 1
Respuesta: Pasar texto de editor iframe RTF a PHP

Muchísimas gracias.
  #5 (permalink)  
Antiguo 15/02/2012, 11:00
 
Fecha de Ingreso: abril-2011
Ubicación: España, Mdrid
Mensajes: 89
Antigüedad: 13 años
Puntos: 1
Respuesta: Pasar texto de editor iframe RTF a PHP

Y ahora... imaginaos que quiero el texto que viaja por una variable $_GET en esa caja de texto.

Es decir http://RTF.com/editor_rtf.php?mostrareneleditor=Lo que quiero mostrar en el editor
  #6 (permalink)  
Antiguo 15/02/2012, 11:18
 
Fecha de Ingreso: abril-2011
Ubicación: España, Mdrid
Mensajes: 89
Antigüedad: 13 años
Puntos: 1
Respuesta: Pasar texto de editor iframe RTF a PHP

¿Puede ser así?

Código PHP:
Ver original
  1. <?php
  2. $obtengo = $_GET["t"];
  3. echo "document.getElementById('edit').contentWindow.document.body.innerHTML = \"".$obtengo."\";";
  4. ?>

Última edición por GuillermoM; 15/02/2012 a las 11:53

Etiquetas: html, iframe, rtf, variables
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 15:03.