Problem loading a string with a lot of information in javascript

0

I create a connection to a mysql database in php and load a variable with information from a field in a bd table, my problem is that when loading a lot of information I can not work with this variable using javascript

include("Conexion.php");

$rs = mysqli_query($con, "SELECT * FROM tbl_publicacion WHERE publicacion_id = $publicacion_id");

while($datos = mysqli_fetch_array($rs)){
    $descripcion = $datos['publicacion_descripcion'];
}

Then in a script block I have this:

var descripcion = "<?php echo $descripcion; ?>";
            alert(descripcion);

If in the field of the table I have little information if it shows me the variable in the alert, but if I have too much information this variable can not work with javascript, it never shows me the alert and when calling this variable in a function this function is never called ...

What can I do to load a variable with the amount of information I want and work with javascript?

  

Clarifying: the variable with the information in php if it loads the data regardless of the size of the text. to show it with echo shows me the data perfectly.   The problem is only by javascript ...

    
asked by Alexis Rodriguez 12.08.2017 в 21:06
source

1 answer

1

I do not think that your problem is that the size of your chain has a lot of information, it can be that as in the chain you save a text description, this can contain line breaks (\ n), when passing this variable from php to js with echo for example, this line break appears causing error.

if you have the code in php:

<?php $descripcion="texto largo...\n...mas texto"; ?>

and in the js you do this:

<script type="text/javascript"> alert('<?php echo $descripcion ?>');</script>

in the browser will appear:

<script type="text/javascript"> alert('texto largo...
...mas texto');</script>

and for this reason the alert will not be executed. You can verify this by doing ctrl + U to see the code in your browser.

To get rid of the line breaks in your variable you can use the following code in php:

$nueva_descripcion=str_ireplace("\n",' ',$descripcion);

This code will replace the line breaks with spaces. In case you are not familiar with the term line break, this occurs when you press "Enter" when writing in a textarea for example, this "Enter" is saved as a character so that the paragraphs of a text are maintained.

I hope it helps. Greetings.

    
answered by 28.03.2018 в 01:51