开发者

escaping greater-than and less-than in php echo inside server-side html

开发者 https://www.devze.com 2023-03-26 21:33 出处:网络
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-

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 h开发者_StackOverflow社区ref="../../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.


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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消