在服务器端html内的php echo中转义大于和小于

发布于 2024-11-28 11:52:45 字数 1653 浏览 0 评论 0原文

在 jQuery 自动完成框中进行选择后,我试图简单地将函数从服务器 php 页面回显给客户端浏览器,以便该函数可以根据需要(客户端)使用自动完成框的值进行处理。自动完成位于 php 页面中,如下所示:

mypage.php

<html>
<head>
    <title>Autocomplete</title>   

    <link href="../../jqSuitePHP/themes/redmond/jquery-ui-1.8.2.custom.css" id="skin" rel="stylesheet" type="text/css" />

    <script src="../../jqSuitePHP/js/jquery-1.6.min.js" type="text/javascript"></script>
    <script src="../../jqSuitePHP/js/jquery-ui-1.8.14.custom.min.js" type="text/javascript"></script>


    <script>
        $(function ac_boxes() {

            $("#dlr").autocomplete({
                source: "dlrAutocompleteSearch.php",
                minLength: 2,
                search   : function(){$(this).addClass('ui-autocomplete-loading');},
                open     : function(){$(this).removeClass('ui-autocomplete-loading');},
                select: function( event, ui ) {

                    // Here's my attempt at calling the client side 'test' function
                    <?php echo '<script>window[test](ui.item.value)</script>;' ?>

                }
            });
        });
    </script>
</head>
<body>
---------
</body>
</html>

但是 <和>造成了问题。如果我删除 <和 >,页面完全处理(当然没有“选择”功能。如果我添加 < 和 >,页面不会处理。

我尝试使用 php htmlentities() 分配字符串,如下所示:

<?php
$val = htmlentities('<script>window[test](ui.item.value)</script>;');
echo $val;
?>

但是这似乎也不起作用。

我的问题是否源于 jQuery 脚本内部的 php?如果是,那么从自动完成的“select”方法调用 php 的另一种方法是什么

I'm trying to simply echo a function back to the client browser from a server php page after a selection has been made in a jQuery autocomplete box so that the function can process as needed (client-side) with the value of the autocomplete box. The autocomplete is in the php page as follows:

mypage.php

<html>
<head>
    <title>Autocomplete</title>   

    <link href="../../jqSuitePHP/themes/redmond/jquery-ui-1.8.2.custom.css" id="skin" rel="stylesheet" type="text/css" />

    <script src="../../jqSuitePHP/js/jquery-1.6.min.js" type="text/javascript"></script>
    <script src="../../jqSuitePHP/js/jquery-ui-1.8.14.custom.min.js" type="text/javascript"></script>


    <script>
        $(function ac_boxes() {

            $("#dlr").autocomplete({
                source: "dlrAutocompleteSearch.php",
                minLength: 2,
                search   : function(){$(this).addClass('ui-autocomplete-loading');},
                open     : function(){$(this).removeClass('ui-autocomplete-loading');},
                select: function( event, ui ) {

                    // Here's my attempt at calling the client side 'test' function
                    <?php echo '<script>window[test](ui.item.value)</script>;' ?>

                }
            });
        });
    </script>
</head>
<body>
---------
</body>
</html>

But the < and > are causing a problem. If I remove the < and >, the page processes completely (without the 'select' function of course. If I add the < and >, the page does not process.

I have tried assigning the string using the php htmlentities() as such:

<?php
$val = htmlentities('<script>window[test](ui.item.value)</script>;');
echo $val;
?>

But this doesn't seem to work either.

Is my problem stemming from the php being inside of the jQuery script? If so, what is another method of calling the php from the 'select' method of autocomplete?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜爱皱眉﹌ 2024-12-05 11:52:45

我不认为这段代码正在做你认为它正在做的事情;当您加载页面时,将执行 PHP,并且最终会在源代码中得到类似以下内容:

<script>
...
select: function( event, ui ) {
    <script>window[test](ui.item.value)</script>;
}
...
</script>

这是不正确的(脚本标记中不需要 script 标记;如您所见它不会做任何事情,只会引起问题)。

如果您想在选择更改时执行一些 PHP,则必须通过 AJAX、提交表单或其他方式再次调用服务器。像这样的东西可能更像你想要的:

select: function(event, ui) {
    // send the selected value to the server for processing
    $.get("processChange.php", {value: ui.item.value});
}

请参阅 $.get( 上的 JQuery 文档 )了解更多相关信息。

另一方面,如果您想要做的只是使用所选值调用另一个客户端 JavaScript 函数(例如 test),则不需要 PHP 来回显任何内容。这应该可以解决问题:

<script>
function test(args) {
    // ...
}

$("#dlr").autocomplete({
    // ...
    select: function(event, ui) {
        test(ui.item.value);
    }
}
</script>

I don't think this code is doing what you think it is doing; when you load the page the PHP is executed and you end up with something like this in the source code:

<script>
...
select: function( event, ui ) {
    <script>window[test](ui.item.value)</script>;
}
...
</script>

Which is not correct (you don't need script tags within script tags; as you've seen it doesn't do anything but cause problems).

If you want to execute some PHP when the selection changes, you have to make another call to the server, via AJAX, submitting a form, or whatever. Something like this might be more like what you want:

select: function(event, ui) {
    // send the selected value to the server for processing
    $.get("processChange.php", {value: ui.item.value});
}

See the JQuery docs on $.get() for more on that.

On the other hand, if all you're trying to do is call another client-side javascript function (test, for example) with the selected value, you don't need PHP to echo anything. This ought to do the trick:

<script>
function test(args) {
    // ...
}

$("#dlr").autocomplete({
    // ...
    select: function(event, ui) {
        test(ui.item.value);
    }
}
</script>
平定天下 2024-12-05 11:52:45

您可以像在 HTML 中一样使用 <>。您还可以使用 replace() 函数查找所有 replace() 函数。和>并更换它们。

You can use < and > just like in HTML. You can also use the replace() function to find all the < and > and replace them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文