从PHP脚本获取Smarty格式的数据

时间:2016-12-14 16:01:35

标签: javascript php xmlhttprequest smarty prestashop

我通过XMLHttpRequest调用的PHP脚本执行查询并检查条件,然后输出带有一些Smarty代码的HTML片段。当我尝试在<div>标记中插入该代码时,通过调用jQuery.html()函数或设置innerHTML属性,Smarty代码按原样打印,因此不会被解释为Smarty代码。我怎么能解决这个问题?

相关代码:

PHP脚本:

<?php
        $mysqli = new mysqli("localhost", "<<<SQL USERNAME>>>", "<<<SQL PASSWORD>>>", "<<<SQL DATABASE>>>");
        $id = $_GET["idmf"];
        $cat = $mysqli->query("<<<SQL QUERY>>>")->fetch_assoc()["category"];
        if ($cat == $_GET["cat"])
            echo 'blahblah';
?>

TPL文件:

{foreach from=$manufacturers item=manufacturer name=manufacturers}
    <div id="mffilter_{$manufacturer.id_manufacturer}"></div>
    <script>

        var xhttpf;
        if (window.XMLHttpRequest)
        {
            xhttpf = new XMLHttpRequest();
        }
        else
        {
            // code for IE6, IE5
            xhttpf = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhttpf.onreadystatechange = function()
        {
            if (this.readyState == 4 && this.status == 200)
            {
                document.getElementById("mffilter_{$manufacturer.id_manufacturer}").innerHTML = this.responseText;
            }
        };
        xhttpf.open("GET", "<<<ENDPOINT>>>.php?cat=food&idmf={$manufacturer.id_manufacturer}", true);
        xhttpf.send();

    </script>

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码替换您的TPL文件代码:

{foreach from=$manufacturers item=manufacturer name=manufacturers}
    <div id="mffilter_"{$manufacturer.id_manufacturer}></div>
    <script>

        var xhttpf;
        if (window.XMLHttpRequest)
        {
            xhttpf = new XMLHttpRequest();
        }
        else
        {
            // code for IE6, IE5
            xhttpf = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhttpf.onreadystatechange = function()
        {
            if (this.readyState == 4 && this.status == 200)
            {
                document.getElementById("mffilter_"{$manufacturer.id_manufacturer}).innerHTML = this.responseText;
            }
        };
        xhttpf.open("GET", "<<<ENDPOINT>>>.php?cat=food&idmf="{$manufacturer.id_manufacturer}, true);
        xhttpf.send();

    </script>