简体   繁体   English

来自PHP和输出缓冲的JavaScript(使用Ajax)

[英]JavaScript (with Ajax) from PHP and Output Buffering

I now have a working JSON formatted file from my PHP scripts. 我现在有一个来自PHP脚本的工作JSON格式的文件。

The next step is to have a JavaScript script to retrieve this data for sorting, filtering and displaying. 下一步是使用JavaScript脚本检索此数据以进行排序,过滤和显示。

I have a working Ajax script that tests ok for pulling back data, but I need to personalize this to the individual. 我有一个工作的Ajax脚本,可以测试是否可以撤回数据,但我需要对个人进行个性化处理。

Within PHP I have a Session variable called MID (Member ID). 在PHP中,我有一个名为MID(会员ID)的Session变量。

I am trying to use PHP to build the JavaScript with the unique URL with the MID as a variable. 我正在尝试使用PHP来构建具有唯一URL的JavaScript,并将MID作为变量。

The following all seem to work except for replacing the midValue variable in the JavaScript text with the MID variable in the outer PHP script. 除了用外部PHP脚本中的MID变量替换JavaScript文本中的midValue变量之外,以下所有内容似乎都有效。

The code so far looks like this ... 到目前为止,代码看起来像这样......



    // 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();

And the getcontent.php file looks like this ... 并且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);
       }

I think something simpler like this should work fine for you. 我认为像这样简单的东西应该适合你。

<?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 ?>

With this method, you are just outputting the javascript and html outside of PHP so you don't need it in tags. 使用此方法,您只是在PHP之外输出javascript和html,因此您不需要在标记中使用它。 Then you can just echo variables or include your header and footer where required. 然后,您可以只回显变量或在必要时包含页眉和页脚。

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

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