Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

388
Visualizações
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

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

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) ?>
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda