0
I have a page that uses a text editor of type 'wysiwyg' which in the case is a textarea element, this generates an html code for text that the user type, I save the value of this field in onblur of the same through an ajax that makes save in SESSION via php, the problem is later, I want to make this text that the user typed appear on the screen (even if the user updates the page), and I have trouble setting the text in the text editor, because the generated code is generated with line break, and by putting this value in javascript gives error due to this line break already looked for ways to remove these line breaks, but n found one that worked.
function that calls ajax:
function SalvaSession(valor){
$.post( "ajax_salva_session.php",
{
'texto' : valor //valor recebe o html do editor de texto
},
function (data) {
}
);
}
ajax_salva_session.php file (which saves the text received from the editor):
<?php
$texto = isset($_POST['texto']) ? $_POST['texto'] : '';
$texto = str_replace("\n", " ", $texto); //usei essas 3 formas que achei
$texto = str_replace("\r", " ", $texto); //na net pra tentar eliminar a quebra
$texto = preg_replace('/\s/',' ',$texto);//de linha, mas até o momento sem sucesso :/
$_SESSION['capitulo'] = $texto;
?>
line that arrow the text in the text editor (the syntax is right, it is the syntax that is in the documentation: $("#placeholder"). Editor("setText", "Text")):
$('#paragrafo').Editor('setText', "<?php echo ($_SESSION['capitulo']); ?>");
text generated in the browser (which gives error):
$('#paragrafo').Editor('setText', "<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: _SESSION in C:\wamp\www\index.php on line <i>10</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0058</td><td bgcolor='#eeeeec' align='right'>241440</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>
</table></font>
");
Two things: you’re using
session_start();
at the beginning of your code? And this is wrong:<?php echo ($_SESSION['capitulo']); ?>
. At the very least<?php echo htmlentities($_SESSION['capitulo']); ?>
, otherwise any occurrence of quotation marks will cause you trouble.– Bacco
I was not using, now I put both, session_start was by inattention even, but I did not know this htmlentities, now you see if I find the error, that still persists.
– alexandre9865
Alexandre, take your line break removals with str_replace, and just use this:
<?php echo nl2br( htmlentities( $_SESSION['capitulo'] ) ); ?>
, depending on the editor can work.– Bacco
i’ve heard of this nl2br, but I think q n can use, because the break of line q occurs from javascript n is a break of line html formatting, and only the way the editor creates html
– alexandre9865
Well, in fact nl2br does not remove breaks, it would be the case to first apply nl2br, and then replace
– Bacco
but then why would I use nl2br if my intention is not to create br? and yes REMOVE line breaks?
– alexandre9865
Your intention is to remove the break because it is giving problem. I’ve thought about solving only the problem, respecting the original text (ie make it work with the break). But without knowing the component you are using, everything is a kick. It’s the kind of thing that should just be some silly little thing getting in the way, but it gets hard to understand the situation without looking closely.
– Bacco
the q component using is the line control editor, its link is https://github.com/suyati/line-control/blob/master/README.md
– alexandre9865
Is there any way to make it work with these line breaks in javascript? because php na vdd ta throwing Session error in html that would be set in the q use editor..
– alexandre9865
but I don’t understand why my Session_start() session is wrong and I set the session $_SESSION['chapter'] = $text
– alexandre9865
That’s why I made my first comment. As long as you don’t fix the mistake, you’re not testing anything. Only that we can not know here what is your line 10 , that has the problem. It may be a $ missing, or some ; wrong, etc. If you can comment on what is on your line 10, it might help.
– Bacco
I managed to remove the line break at last! thanks for all the help, your tips helped, I think the biggest problem was I n declared session_start() at the beginning and he saved in Sesssion the php error code, and then gave error all the time, Now I changed what is stored in Session and I was able to fix it by this error, thanks again for the tips @Bacco!
– alexandre9865
If you were able to solve the problem accept the answer that helped or put a new answer if the solution is not in the answers.
– Sergio