Error backoffice.js: 128 Uncaught TypeError: Can not read property 'replace' of null (...)

0

When using the function replace() I get this error. I am using it because I am generating html code with user input values with a button.

The problem is that if the user enters double quotes (") or simple (') the code is not generated correctly.

With the replacre("'", "''") function I can replace those single quotes

but I get this error

  

backoffice.js: 128 Uncaught TypeError: Can not read property 'replace'   of null (...)

I insert the part of the code to see if you see the error.

function modificarSugerencia(id){
     $.get("modules/backoffice/sugerencias/leer_sugerencia.php?idSugerencia=" + id, function(data){
        if(data)
        {
            json = JSON.parse(data);

            //console.log(data);
            console.log(json.sugerencia.CUERPO_SUGERENCIA);
            

            url="modules/backoffice/sugerencias/rellenadoSugerencias.php";
            urlReturn="modules/backoffice/sugerencias/gestion_sugerencias.php";
            var boxhtml ="";
            boxhtml+="<form action='' name='comunicationUpdate' id='comunicationUpdate' method='post'";
            boxhtml+="<div id='box-modificar-comunicado' class='capa-fondo-caja capa-fondo-caja-backoffice centered-absolute'>";
            boxhtml+="<div class='header-caja-fondo-backoffice'>"

            boxhtml+="<h2><span class='icon-document-edit size-icons-table-backoffice'></span>Modificación sugerencia</h2>";
            boxhtml+="</div>";

            boxhtml+="<div id='form-main'>";
            boxhtml+="<p class='asunto'>Asunto:";
            boxhtml+="<input name='asunto' type='text' class='feedback-input' placeholder='Asunto' id='asunto' value='"+json.sugerencia.ASUNTO.replace("'","'")+"' />";
            boxhtml+="</p>";

            boxhtml+="<p class='fechas'>Descripcion mejora:";    
            boxhtml+="<input name='cuerpoSugerencia' type='text' class='feedback-input' value='"+json.sugerencia.CUERPO_SUGERENCIA.replace("'","'")+"' />";
            boxhtml+="</p>";

            boxhtml+="<p class='text'>Respuesta:";
            boxhtml+="<input name='respuesta' type='text' class='feedback-input' id='comment' value='"+json.sugerencia.RESPUESTA.replace("'","'")+"' placeholder='Comunicado'>";
            boxhtml+="</p>";

          
    
asked by PictorGames 21.12.2016 в 11:02
source

2 answers

2

To avoid errors with data null you should check them

Javascript:

if ( typeof(json.sugerencia.ASUNTO) !== "undefined" &&  json.sugerencia.ASUNTO !== null ) {
       boxhtml+="<div id='form-main'>";
       boxhtml+="<p class='asunto'>Asunto:";
       boxhtml+="<input name='asunto' type='text' class='feedback-input' placeholder='Asunto' id='asunto' value='"+json.sugerencia.ASUNTO.replace("'","'")+"' />";
       boxhtml+="</p>";
}else {
      //Error
}
    
answered by 21.12.2016 / 12:01
source
-1

To escape single or double quotes you must use \ . Ex:

replace("\'","´´")
    
answered by 21.12.2016 в 12:09