简体   繁体   English

PHP Inside JavaScript-报价问题?

[英]PHP Inside JavaScript - Quotes Issue?

Good afternoon, 下午好,

I've been looking around for info on this and have come up dry. 我一直在寻找有关此方面的信息,并且干dry了。 The idea is to have a form that uses select boxes to display info from a mysql database. 想法是要有一个使用选择框显示mysql数据库信息的表单。 This is mainly done with html/php, but I have added JavaScript functionality that allows a user to press a button to create an additional array of selects to indicate additional products. 这主要是通过html / php完成的,但是我添加了JavaScript功能,该功能允许用户按下按钮来创建其他选择数组,以表示其他产品。 The problem I am currently facing is that I cannot seem to get the php to work inside of the JS innerHTML. 我目前面临的问题是,我似乎无法使php在JS innerHTML内部运行。

order-add.php 订单add.php

<script src="/js/addProduct.js" language="Javascript" type="text/javascript"></script>

    <tr>
        <td>Product(s)</td>
        <td><div id="dynamicInput">
            <select name="orders_product[]">
                <option value="">Select One</option>
                <?php
                        $orders_product_query = $conn->prepare("SELECT product_name FROM product");
                        $orders_product_query->execute();
                        $orders_product_query->bind_result($orders_product_result);
                        while ($orders_product_query->fetch()) {
                            echo "<option value = '$orders_product_result'>$orders_product_result</option>";
                        }
                        $orders_product_query->close();
                ?>
            </select>
            </div>
            <input type="button" value="Add product" onClick="addInput('dynamicInput');">
        </td>
    </tr>

addProduct.js addProduct.js

var counter = 1;
var limit = 10;
function addInput(divName) {
if (counter == limit) {
    alert("You have reached the limit of adding " + counter + " products.");
} else {
    var newdiv = document.createElement('div');
    newdiv.innerHTML = "
        <select name='orders_product[]'>
        <option value=''>Select One</option>
        <?php
            $orders_product_query = $conn->prepare('SELECT product_name FROM product');
            $orders_product_query->execute();
            $orders_product_query->bind_result($orders_product_result);
            while ($orders_product_query->fetch()) {
                echo 'test';
            }
            $orders_product_query->close();
        ?>
        </select>";
    document.getElementById(divName).appendChild(newdiv);
    counter++;
    }
}

With all of the testing I've been trying to run on this, it appears that the issue is with the newdiv.innerHTML portion of the addProduct.js. 在我一直试图进行的所有测试中,似乎问题出在addProduct.js的newdiv.innerHTML部分。 I'm thinking that it is not accepting php, but it also could be that I have the inner quotes somehow messed up. 我以为它不接受php,但也可能是我的内引号弄乱了。

Thanks in advance for your help! 在此先感谢您的帮助!

I think you are not understanding how this works. 我认为您不了解这是如何工作的。 PHP code is executed when the client requests the page on the server side. 当客户端在服务器端请求页面时,将执行PHP代码。 JS is executed on the client side. JS在客户端执行。 There is no PHP interpreter on the web browser. Web浏览器上没有PHP解释器。 What you could do is putting the PHP code in a separate file (Example: fragment.php) and calling it through AJAX. 您可以做的就是将PHP代码放在一个单独的文件中(例如:fragment.php),并通过AJAX对其进行调用。 In that case your code will be executed in the server. 在这种情况下,您的代码将在服务器中执行。

I hope this helps! 我希望这有帮助!

Is addProduct.js included as a tag? 是否将addProduct.js包含在标签中? If so, take a look in your developer tools. 如果是这样,请查看您的开发人员工具。 This is probably showing exactly as you have it posted here, with the php tag as part of the text. 这可能与您在此处发布的内容完全一样,并且php标签是文本的一部分。 You'll need to change the extension of this file to .php and change it in the script tag as well to get php to parse the file. 您需要将此文件的扩展名更改为.php,并在script标签中进行更改,以使php解析该文件。

Alternatively, you can try somethign like what @NMO suggested, and retrieve this via AJAX. 另外,您可以尝试类似@NMO的建议,然后通过AJAX进行检索。

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

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