ReferenceError: "X" is not defined in javascript

Hi everyone,

I have a little code where if I press over the form the database is updated through AJAX. It was running well but I wanted to change the variable I was sending through AJAX.

HTML code:

if ($user != "null") {
                $usuario =  $_SESSION['usuario'];
                $statement = $conexion->prepare("SELECT * FROM users WHERE usuario = :usuario");
                $statement->execute(array(":usuario" => $usuario));
                $user_games_following = $statement->fetch();

                $porciones = explode(";", $user_games_following['siguiendo_juegos']);

                //buscamos el código del juegos
                $statement = $conexion->prepare("SELECT * FROM info_XboxOne WHERE id = :id");
                $statement->execute(array(":id" => $en_que_ficha));
                $codigo_juego = $statement->fetch();

                $contador = 0;
                $codigo = $codigo_juego['Codigo_juego'];

                foreach ($porciones as $porcion) {
                    if ($porcion ==  $codigo) {
                        $contador++;
                    }
                }

                if ($contador == 0) {
                    echo "<form class='form_suscripcion col_400' name='suscribe' id='suscribirse_" . $en_que_ficha . "' title='Suscripcion post' action='single.php?ID=" . $en_que_ficha . "' method='post'>";
                        echo "<p id='suscripcion_previa_" . $en_que_ficha . "'>Agrega el juego a favoritos</p>";
                        echo "<p class='oculto' id='respuesta_sus_" . $en_que_ficha . "''></p>";
                        echo "<input class='suscribe_box' type='checkbox' id='suscribe_" . $en_que_ficha . "' name='subs'>";
                        echo "<label id='suscripcion_" . $en_que_ficha . "'  class='suscribirse' onclick='myFunction_suscribirse_juego($codigo)' for='suscribe_" . $en_que_ficha . "'>Agregar a favoritos</label>";
                        echo "<label id='suscripcion_oculto_" . $en_que_ficha . "'  class='label_oculto' onclick='myFunction_suscribirse_juego($codigo)' for='suscribe_" . $en_que_ficha . "'>Quitar de favoritos</label>";
                } else {
                    echo "<form class='form_suscripcion col_400' name='suscribe' id='suscribirse_" . $en_que_ficha . "' title='Suscripcion post' action='single.php?ID=" . $en_que_ficha . "' method='post'>";
                        echo "<p id='suscripcion_previa_" . $en_que_ficha . "'>Quitar juego de favoritos</p>";
                        echo "<p class='oculto' id='respuesta_sus_" . $en_que_ficha . "''></p>";
                        echo "<input class='suscribe_box' type='checkbox' id='suscribe_" . $en_que_ficha . "' name='subs' checked>";
                        echo "<label  id='suscripcion_" . $en_que_ficha . "'  class='label_oculto' onclick='myFunction_suscribirse_juego($codigo)' for='suscribe_" . $en_que_ficha . "'>Agregar a favoritos</label>";
                        echo "<label  id='suscripcion_oculto_" . $en_que_ficha . "'  class='suscribirse' onclick='myFunction_suscribirse_juego($codigo)' for='suscribe_" . $en_que_ficha . "'>Quitar de favoritos</label>";
                }

                echo "</form>";
            /* fin boton suscripcion */
            }

Javascript part:

<script>
        function myFunction_suscribirse_juego(respuesta_id) {
            let suscribirse;
            var id_respuesta = respuesta_id;
            console.log(id_respuesta);
               var d = document.getElementById("suscripcion_"+id_respuesta);
               var label_2 = document.getElementById("suscripcion_oculto_"+id_respuesta);
               var form = document.getElementById("suscribirse_"+id_respuesta);
               var respuesta_id = "respuesta_sus_" + respuesta_id;
               var a_ocultar = "suscripcion_previa_" + id_respuesta;
               var c = document.getElementById(a_ocultar);
               var mostrar_resultados = document.getElementById("heart_"+id_respuesta);
               $(mostrar_resultados).removeClass("oculto");

               c.className += " ocultar";

               if( $("#suscribe_"+id_respuesta).prop('checked') ) {
                  suscribirse = 1;

                  label_2.className += " label_oculto"
                  $(label_2).removeClass("suscribirse")
                  d.className += " suscribirse"
                  $(d).removeClass("label_oculto")

                } else {
                  suscribirse = 0;
                   console.log("hola");
                   label_2.style.background = "red";
                   d.className += " label_oculto"
                   $(d).removeClass("suscribirse")
                   label_2.className += " suscribirse"
                   $(label_2).removeClass("label_oculto")

                }

                console.log(id_respuesta)

               $.ajax ({
                 type: 'POST',
                 url: '<?php echo $ruta ?>proces_suscribe_juego.php',
                 data: { "corazon": suscribirse, "id_respuesta":id_respuesta },
                 success:function(datos){
                   $("#"+respuesta_id).html(datos);
                 }
               });
          };
        </script>

I don’t understand why is not working now. I was sending the ID, and now I want to send a code, which for example is bzjh18qjvdvw but it tells me on the console the following error:

ReferenceError: bzjh18qjvdvw is not defined

I don’t really understand what is happening…

Hi @egoicantero, I’m not sure what the problem is either but it seems that the code is treating ‘bzjh18qjvdvw’ as a variable instead of as a string. Does the error point you to any specific code line?

Is pointing to the first line if I´m not wrong: http://localhost/xboxone/Juegos/Xbox-One/553/IDARB/:1:1 but it’s nonsense.

Maybe you can view the page source and look for your id string ‘bzjh18qjvdvw’ and if you find it then post the surrounding code

This is what I get on the HTML:

<label id="suscripcion_16" class="suscribirse" onclick="myFunction_suscribirse_juego(c3kldkzbhncz)" for="suscribe_16">Agregar a favoritos</label>

Got it, your id c3kldkzbhncz should be within single quotes : ‘c3kldkzbhncz’

I´m trying with this code but is not working:

echo "<label id='suscripcion_" . $en_que_ficha . "'  class='suscribirse' onclick='myFunction_suscribirse_juego('". $codigo . "')' for='suscribe_" . $en_que_ficha . "'>Agregar a favoritos</label>";

Because you should not use single quotes all round, please see the below example

onclick="functionCall('myid')" ...

The following will not work:

onclick='functionCall('myid')' ...

Note in php you can escape double quotes within a string with the backwards slash "

1 Like

Yes but I´m using PHP, but I have used finally this:

echo "<label id='suscripcion_" . $en_que_ficha . "' class='suscribirse' onclick=\"myFunction_suscribirse_juego($en_que_ficha,'". $codigo . "')\" for='suscribe_" . $en_que_ficha . "'>Agregar a favoritos</label>";

But is not working properly. It is supposed that if I press on the button the second time, it should erase the code from the database.

Sorry there it is. I was ok but I hadn’t change in the for lines I had to. Worked!! Thanks!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.