Jquery .val () fails to read line break

1

I have to fill in a textarea and I'm using the function .val () of Jquery, when there is text in a line there is no problem, but having 2 lines gives "Unexpected EOF" syntax error in jquery line 349 .

what could it be?

the data are lame from mysql and are in utf8_general_ci format

the code is this:

$('#art_detalles').val('<?=$DMoarticulos["detalles"]; ?>');

and the html code this:

<tr>
   <td class="derecha_listados"><label>Detalles:</label></td>
   <td class="izquierda_listados"><textarea id="art_detalles" name="art_detalles"></textarea></td>
</tr>

If I put a console.log with the php variable it gives this error: SyntaxError: Unexpected identifier 'and' also on line 349 of jquery And it's the first character after the line break.

with var_dump I get this message:

string(14) "ola
y k ase"

Is there any way to force it to read the content of the variable as if it were html?

    
asked by Killpe 18.01.2017 в 20:21
source

2 answers

0

Only needed to put preg_replace("/\r\n|\r|\n/",'\n',$DMoarticulos["detalles"]); and everything solved.

    
answered by 03.02.2017 / 23:10
source
2

Sometimes there are non-visible characters, also known as Byte Order Mark or simply (BOM) , which has characters that are not printable by definition in the Unicode system.

The mysterious invisible character:

We analyze the string: "olay k ase" :

$bom = "olay k ase";

$detect_encoding = mb_detect_encoding($bom);
echo $detect_encoding.PHP_EOL; 
// Resultado => UTF-8

$convert_encoding = mb_convert_encoding($bom, "ASCII", $detect_encoding);
echo $convert_encoding.PHP_EOL; // => 
// Resultado => ola?y k ase

Result 1

The result is that it has the UTF-8 encoding and the ? character and the BOM representation in Hexadecimal would be EF BB BF

Now, this is the only solution (and working) that I've found to remove in PHP representations < strong> BOM :

preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);

Result 2

We check with your code to see if it works, with and without BOM :

with BOM :

$('#art_detalles').val("ola
    y k ase");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="art_detalles" name="art_detalles"></textarea>

without BOM

$('#art_detalles').val("olay k ase");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="art_detalles" name="art_detalles"></textarea>

Where does the BOM come from?

  

It depends how you have the configuration of your editor, I may add you   by default the BOM , and apparently also if you copy some file that was not created by you, or you are testing on a different server with another system.

How to solve?

  

Edit the file with an editor that allows you to save   UTF-8 format without BOM, if you can not change it, change the editor. Notepad++ for example allows you this operation.

Or the solution in PHP to eliminate the BOM representations.

    
answered by 18.01.2017 в 22:27