简体   繁体   English

我在 javascript 文件中的 php echo 中出错

[英]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.当我在 JavaScript 文件中使用一些代码但插入 HTML div 时出现错误。 I used a PHP method to show all clients, and is working fine in html page.我使用 PHP 方法来显示所有客户端,并且在 html 页面中工作正常。 But in JavaScript I get error in the echo.但是在 JavaScript 中,我在回声中出错。

See error in Visual Studio Code ==> https://prnt.sc/1yy4sxl请参阅 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 ' ' 中删除了 '',如下所示:

'  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并且在这个手动表单中运行正常,但动态我无法处理它

You have mixex PHP code lines inside the JavaScript single qoutes.您在 JavaScript 单个 qoutes 中有 mixex PHP 代码行。 PHP should run as a separate block but you can generate the JavaScript single quotes using the PHP ECHO itself: PHP 应该作为单独的块运行,但您可以使用 PHP ECHO 本身生成 JavaScript 单引号:

$(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>'
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM