简体   繁体   English

JS内部HTML内的PHP圈

[英]PHP circle inside JS inner Html

When I press the button it should add one more div, like the one below, calling JS function called comida_campos() . 当我按下按钮时,它应该再添加一个div,如下所示,调用JS函数comida_campos()

<div class="panel panel-default">
    <div class="panel-body">
        <div id="comida_campos">
        </div>
        <div class="col-sm-6 nopadding">
            <div class="form-group">
                <input id="quantidade" class="form-control"name="quantidade[]" placeholder="Quantidade/g" type="text" value="">
            </div>
        </div>
        <div class="col-sm-6 nopadding">
            <div class="form-group">
                <div class="input-group" id="replicate"> 

                    <select class="form-control" name="alimento[]">
                        <option value="">Alimento</option>
                        <script>
                            document.write('<?php
                                $it=0;
                                while($it <= 9){

                                echo '<option value="'.$array_of_aliments[$it]['Idalimento'].'">'.$array_of_aliments[$it]['alimento'].'</option>';
                                $it++;
                                }
                            ?>');
                        </script>
                    </select>
                    <div class="input-group-btn">
                        <button class="btn btn-success" onclick="comida_campos();" type="button">
                            <span aria-hidden="true" class="glyphicon glyphicon-plus"></span>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

Result of the code 代码结果

IMG

Here is the JS fuction: 这是JS功能:

var room = 1;
function comida_campos() {
    room++;
    var objTo = document.getElementById('comida_campos')
    var divtest = document.createElement("div");
    divtest.setAttribute("class", "form-group removeclass" + room);
    var rdiv = 'removeclass' + room;
    divtest.innerHTML = '<div class="col-sm-6 nopadding"><div class="form-group"> <input type="text" class="form-control" id="Degree" name="Degree[]" value="" placeholder="Quantidade/g"></div></div><div class="col-sm-6 nopadding"><div class="form-group"><div class="input-group"> <select class="form-control" id="educationDate" name="alimento[]"><option value="">Date</option><option value="2015">2015</option><option value="2016">2016</option><option value="2017">2017</option><option value="2018">2018</option> </select><div class="input-group-btn"> <button class="btn btn-danger" type="button" onclick="remove_education_fields(' + room + ');"> <span class="glyphicon glyphicon-minus" aria-hidden="true"></span> </button></div></div></div></div><div class="clear"></div>';

    objTo.appendChild(divtest)
}

The problem is that I want to insert my PHP while circle is inside the divtest.innerHtml , and I don't have any idea how to do this. 问题是我想在circle在divtest.innerHtml内时插入我的PHP,而我不知道该怎么做。

PHP is server side, JS is client (browser) side. PHP是服务器端,JS是客户端(浏览器)端。 Server side script (PHP in this case) gets executed before response is sent to the client. 在将响应发送到客户端之前,将执行服务器端脚本(在这种情况下为PHP)。 Once client has received the response, JS is executed in the browser and has no more access to PHP interpreter. 客户端收到响应后,将在浏览器中执行JS,并且无法再访问PHP解释器。

The simple answer is: You can not run PHP from Javascript. 简单的答案是:您不能从Javascript运行PHP。

The more correct answer is: look in to async JS requests (AJAX). 正确的答案是:查看异步JS请求(AJAX)。 This allows you to make separate requests to the server while executing JS. 这使您可以在执行JS时向服务器发出单独的请求。 https://en.wikipedia.org/wiki/Ajax_(programming) - this will give you a basic understanding and a starting point. https://zh.wikipedia.org/wiki/Ajax_(编程) -这将为您提供基本的了解和起点。

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

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