简体   繁体   English

php in resultStr 在回调jquery函数中,不起作用

[英]php in resultStr in callback jquery function, not working

I have a view, the controller renders the view sending some data, example $sizes.我有一个视图,控制器渲染视图发送一些数据,例如 $sizes。 In the view I have a jquery function that based on form post retrieves objects from database and returns a json response.在视图中,我有一个基于表单发布的 jquery 函数从数据库中检索对象并返回一个 json 响应。 in the callback function I append a resultStr so to update the view with the item I got from the database.在回调函数中,我附加了一个 resultStr 以便使用我从数据库中获得的项目更新视图。 the problem was I am using laravel and blade and blade doesn't get parsed if the script is separated from the view.问题是我正在使用 laravel 和刀片,如果脚本与视图分开,则刀片不会被解析。 So I thought, ok I will have the resultStr use plain php instead but it doesn't work either.所以我想,好吧,我会让 resultStr 使用普通的 php 来代替,但它也不起作用。 For example I put this code in the callback function:例如我把这段代码放在回调函数中:

resultStr = resultStr +

      "<?php " +
      "foreach ($possible_sizes as $size){" +
      "print(\" {$size[\"it\"]} \")}" +
      " ?>";

$("#results").html(resultStr);

but then what I get in my div is not the data but this line:但是然后我在我的 div 中得到的不是数据而是这一行:

<div id="results">

<!--?php foreach ($possible_sizes as $size){print(" {$size["it"]} ")} ?-->

</div>

question is: why?问题是:为什么?

example of code in script.js script.js 中的代码示例

    jQuery(function ($) {
        $(document).ready(function () {
            var form = $('form');
            form.submit(function (e) {
                e.preventDefault();
                $.ajax({
                    url: form.prop('action'),
                    type: 'post',
                    dataType: 'json',
                    data: form.serialize(),
                    success: function (data) {

                        var obj = (data);
                        var resultStr = "";
                        for (var i = 0; i < obj.length; i++) {

                            resultStr = resultStr + 
                            "string to be" + 
                            "inserted in the #results" +
                            "div and containing php code" +
                            "to be executed in the view";


                        }

                        $("#results").html(resultStr);

                    }

                })
            });

        });
    });

Based on the long comment thread above, it seems the answer is...根据上面的长评论线程,答案似乎是......

You have a PHP page which is referencing a .js file.您有一个引用.js文件的 PHP 页面。 In that separate .js file you have the following:在那个单独的.js文件中,您有以下内容:

resultStr = resultStr +
      "<?php " +
      "foreach ($possible_sizes as $size){" +
      "print(\" {$size[\"it\"]} \")}" +
      " ?>";

$("#results").html(resultStr);

There is a series of significant problems here...这里有一系列重大问题......

  1. JavaScript runs on the client, after the server-side (PHP) code has already been processed . JavaScript 在客户端上运行,服务器端 (PHP) 代码已经被处理之后 You can't inject more PHP code client-side because the server isn't going to process it.您不能在客户端注入更多 PHP 代码,因为服务器不会处理它。 To the browser, this is just text.对于浏览器来说,这只是文本。 Nothing will actually execute it.没有什么会实际执行它。
  2. Even if you want to execute the PHP code before injecting its results into your HTML, .js files aren't processed by the server-side PHP interpreter.即使您想在将结果注入 HTML 之前执行 PHP 代码,服务器端 PHP 解释器也不会处理.js文件。 You might be able to configure that differently, but I really wouldn't recommend it.可能可以进行不同的配置,但我真的不推荐它。 Keep the PHP in the .php pages.将 PHP 保留在.php页面中。 If you want to inject a value into the JavaScript, render that value in a script block on the page and use it to invoke functionality in the .js file.如果要将值注入 JavaScript,请在​​页面上的script块中呈现该值,并使用它来调用.js文件中的功能。
  3. Even if you do configure your server to process PHP in .js files, this PHP code is going to be one giant syntax error.即使您确实将服务器配置为处理.js文件中的 PHP,此 PHP 代码也将是一个巨大的语法错误。 JavaScript can't be used to build PHP code like this. JavaScript 不能用于构建这样的 PHP 代码。 (It can be used to build strings which look like PHP code, but in the browser those are just strings.) (它可用于构建看起来像PHP 代码的字符串,但在浏览器中它们只是字符串。)

PHP code is processed first, once , on the server . PHP 代码首先在服务器上处理一次 The results of that code are sent to the browser as a web page.该代码的结果作为网页发送到浏览器。 From the moment the page loads in the browser, PHP is done .从页面在浏览器中加载的那一刻起,PHP 就完成了 You can make further AJAX requests to the server to invoke further PHP operations and get the results of those operations.您可以向服务器发出进一步的 AJAX 请求以调用进一步的 PHP 操作并获取这些操作的结果。 But you can't run PHP code in the browser .但是你不能在浏览器中运行 PHP 代码。

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

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