Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

278
Vistas
Uncaught SyntaxError: Invalid or unexpected token in js

I know this question already asked several times before. But i could not get related to me .I am working on chat system where i am sending and receiving messages.

My PHP code is :-

     <div class="chat-area" id="chatBox">
                    <?php
                    if (!empty($chats)) {
                        foreach ($chats as $chat) {
                            $timeago = get_timeago(strtotime($chat['created_at']));
                            $msg_time = htmlspecialchars(trim($timeago));
                            if ($chat['from_id'] == $_SESSION['login']) { ?>
                                <div class="outgoing">
                                    <div class="details">
                                        <p><?= $chat['message'] ?> <h><?= $msg_time ?></h>
                                        </p>
                                    </div>
                                </div>
                            <?php } else { ?>
                                <div class="incoming">
                                    <div class="details">
                                        <p> <?= $chat['message'] ?> <h><?= $msg_time ?></h>
                                        </p>
                                    </div>
                                </div>
                        <?php }
                        }
                    }

My js code is :-

    $(document).ready(function() {
            $("#sendBtn").on('click', function() {
                message = $("#message").val();
                if (message == "") return;

                $.post("app/ajax/insert.php", {
                        message: message,
                        to_id: <?= $unid ?>},
                    function(data, status) {
                        $("#message").val("");
                        $("#chatBox").append(data);
                        scrollDown();
                    });
            });

But the problem is when

     to_id: <?= $unid ?>

$unid is numeric like 12345 it working fine but when $unid is albhanumeric like 1b3vc3g its showing mention error. It is behaving same for send and receiving. Please help me to tackle this . thanks in advance

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use json_encode, which will output the correct form for JavaScript as long as the parameter is of one of the JSON-friendly types:

to_id: <?= json_encode($unid) ?>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda