Ver Mensaje Individual
  #3 (permalink)  
Antiguo 06/03/2020, 12:35
minombreesmm
 
Fecha de Ingreso: agosto-2012
Ubicación: M.
Mensajes: 2.031
Antigüedad: 11 años, 8 meses
Puntos: 52
Respuesta: Asignar cadena php multiline a textarea con javascript?

Cita:
Iniciado por Panino5001 Ver Mensaje
Puedes usar json_encode para escapar tu texto:
Código PHP:
<?php
header
("Content-type:text/html;charset=utf-8");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="format-detection" content="telephone=no">
<title>test</title>
<?php
$txt
='hola,

¿cómo va todo?'
;
?>
<script>
/*alert("<?php echo $txt?>");Falla: Uncaught SyntaxError: Invalid or unexpected token*/
alert(<?php echo json_encode($txt); ?>);//no falla
</script>
</head>
<body>

</body>
</html>
Gracias, parece que si funcionó asi, ahora falta probar con un texto que tiene divs y codigos de imagenes etc

Código HTML:
Ver original
  1. <?php
  2. header("Content-type:text/html;charset=utf-8");
  3. ?>
  4. <!DOCTYPE html>
  5.     <head>
  6.         <meta charset="UTF-8">
  7.         <meta name="viewport" content="width=device-width, initial-scale=1">
  8.         <meta name="format-detection" content="telephone=no">
  9.         <script src="jquery-3.4.1.min.js"></script>
  10.         <title>test</title>
  11.         <?php
  12.        $txt='<div>hola,
  13.  
  14. ¿cómo va todo?</div>';
  15.         ?>
  16.         <script>
  17.             $(document).ready(function() {
  18.             /*alert("<?php //echo $txt; ?>");Falla: Uncaught SyntaxError: Invalid or unexpected token*/
  19.             //alert(<?php //echo json_encode($txt); ?>);//no falla
  20.                 $("#texto").val(<?php echo json_encode($txt); ?>);
  21.             });
  22.         </script>
  23.     </head>
  24.     <body>
  25.        
  26.         <textarea id="texto" rows="4" cols="50">
  27.            
  28.         </textarea>
  29.  
  30.     </body>
  31. </html>
__________________
a veces creemos que es algo básico pero al profundizar nos damos cuenta que estábamos tocando solo la cola de la culebra