Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!