1

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Reichlich
  • 314
  • 2
  • 7
  • Are you running this in a javascript file, or this is in php? – Jay Hewitt Nov 10 '21 at 00:01
  • You can't run php code inside a javascript file. – noah1400 Nov 10 '21 at 00:02
  • Well, I was trying to create a select, and after you choose the new order type, in a js file I will run the php in order to show a list of clients. THis is to show a new select with the list of all clients – Reichlich Nov 10 '21 at 01:46
  • For the php portion of the code that is generating all the options, all the single quotes need to be removed for the ` – Paul T. Nov 10 '21 at 02:16
  • Yes, this is a js file – Reichlich Nov 10 '21 at 21:52
  • it works ok, using php code, but without variables, something like this:` ----------------------------------------------------------- ' $value) {+ ' 'echo ' + ' ;' + '?>' + – Reichlich Nov 10 '21 at 21:54
  • You can use PHP to generate JS, which will be sent to the browser; but you cannot use JS to generate PHP, because the server has already finished its job. – IMSoP Nov 14 '21 at 19:20

1 Answers1

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>'
)
Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82