JavaScript(带Ajax)来自PHP和输出缓冲

JavaScript (with Ajax) from PHP and Output Buffering

本文关键字:PHP 输出 缓冲 来自 Ajax JavaScript      更新时间:2023-09-26

我现在有一个工作JSON格式的文件从我的PHP脚本。

下一步是使用一个JavaScript脚本来检索这些数据,以便进行排序、过滤和显示。

我有一个工作的Ajax脚本,测试ok拉回数据,但我需要个性化这到个人。

在PHP中,我有一个名为MID (Member ID)的会话变量。

我试图使用PHP来构建JavaScript与唯一的URL与MID作为一个变量。

除了将JavaScript文本中的midValue变量替换为外部PHP脚本中的MID变量之外,以下内容似乎都可以工作。

目前为止的代码是这样的…


    // This is a PHP file
    // Setup PHP Output Buffering to change the MID value
    session_start();
    $MID = $_SESSION['MID'];
    function callback($buffer)
    {
      return (str_replace("midValue", $MID, $buffer));
    }
    ob_start("callback");
/*
Some bits I can't show as I haven't figured out the correct Stackoverflow tags (!) ...
 - Add the usual HTML tags such as `HTML, HEAD, TITLE, BODY, SCRIPT` etc
 - Include a DIV with an ID of **json**, this will be replaced by the JSON output it
   self.
 - Enclose the params variable with the `CDATA` tags to maintain the ampersand.
*/
    params = "url=server.com/content.php?action=json&MID=" + midValue
    request = new ajaxRequest()
    request.open("POST", "getcontent.php", true)
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
    request.setRequestHeader("Content-length", params.length)
    request.setRequestHeader("Connection", "close")
    request.onreadystatechange = function()
    {
        if (this.readyState == 4)
        {
            if(this.status == 200)
            {
                if(this.responseText != null)
                {
                    document.getElementById('json').innerHTML = this.responseText
                }
                else alert("Ajax Error: No data recieved")
            }
            else alert("Ajax Error: " + this.statusText)
        }
    }
    request.send(params)
    function ajaxRequest()
    {
        try
        {
            var request = new XMLHttpRequest()
        }
        catch(e1)
        {
            try
            {
                request = new ActiveXObject("Msxml2.XMLHTTP")
            }
            catch(e2)
            {
                try
                {
                    request = new ActiveXObject("Microsoft.XMLHTTP")
                }
            catch(e3)
                {
                    request = false
                }
            }
    }
    return request
   }
/*
Add the closing `SCRIPT, BODY and HTML` tags here.
*/
    ob_end_flush();

getcontent.php文件是这样的…


       if(isset($_POST['url'])) {
            echo file_get_contents("http://" . SanitizeString($_POST['url']));
       }
       function SanitizeString($var) {
           $var = strip_tags($var);
           $var = htmlentities($var);
           return stripslashes($var);
       }

我想这样简单的方法对你来说应该没问题。

<?php
session_start();
$MID = $_SESSION['MID'];
?>
params = "url=server.com/content.php?action=json&MID=<?php echo $MID ?>"
request = new ajaxRequest()
request.open("POST", "getcontent.php", true)
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
request.setRequestHeader("Content-length", params.length)
request.setRequestHeader("Connection", "close")
... // rest of javascript
<?php include 'footer.php'; // include footer code here ?>

使用此方法,您只是输出PHP之外的javascript和html,所以您不需要在标签中。然后,你可以只是回显变量或在需要的地方包含页眉和页脚。