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

192
Visualizações
I get error in a php echo inside a javascript file

I am getting error when I use some code inside a JavaScript file, but inserting into HTML div. I used a PHP method to show all clients, and is working fine in html page. But in JavaScript I get error in the echo.

See error in Visual Studio Code ==> https://prnt.sc/1yy4sxl

Here is my code:

        $(this).parent().parent().parent().children(".clientNewOrder").html(

'<div class="form-group">' +
'    <div class="input-group mb-3">' +
'        <div class="input-group-prepend">' +
'            <select class="form-control" required>' +
'                <option value="">Select Client</option>' +
'                <?php' +
'                $item = null;' +
'                $valor = null;' +
'                $selectClient = ControllerClients::ctrReadClients($item, $values);' +
'                foreach ($selectClient as $key => $value) {' +
'                    echo <option value=".$value["id"]. ">'.$value["name"]. '</option>;' +
'                }' +
'                ?>' +
'            </select>' +
'        </div>' +
'    </div>' +
'</div>'
)

I tried changing "" '' but problem still here. I removed '' in echo ' ', something like this:

'  echo <option selected>Select</option>' +
'       <option value="01">John</option>' +
'        <option value="02">Mary</option>;' +

and is running ok in this manual form, but dynamically I can't handle it

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have mixex PHP code lines inside the JavaScript single qoutes. PHP should run as a separate block but you can generate the JavaScript single quotes using the PHP ECHO itself:

$(this).parent().parent().parent().children(".clientNewOrder").html(

'<div class="form-group">' +
'    <div class="input-group mb-3">' +
'        <div class="input-group-prepend">' +
'            <select class="form-control" required>'+
'                <option value="">Select Client</option>'
                <?php
                $item = null; 
                $valor = null;
                $selectClient = ControllerClients::ctrReadClients($item, $values);
                foreach ($selectClient as $key => $value) {
                    echo '+\'<option value="'.$value["id"]. '">' .$value["name"]. '</option>\'+'; 
                }
                ?>
'            </select>' +
'        </div>' +
'    </div>' +
'</div>'
)
about 4 years ago · Juan Pablo Isaza 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