簡體   English   中英

來自PHP和輸出緩沖的JavaScript(使用Ajax)

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

我現在有一個來自PHP腳本的工作JSON格式的文件。

下一步是使用JavaScript腳本檢索此數據以進行排序,過濾和顯示。

我有一個工作的Ajax腳本,可以測試是否可以撤回數據,但我需要對個人進行個性化處理。

在PHP中,我有一個名為MID(會員ID)的Session變量。

我正在嘗試使用PHP來構建具有唯一URL的JavaScript,並將MID作為變量。

除了用外部PHP腳本中的MID變量替換JavaScript文本中的midValue變量之外,以下所有內容似乎都有效。

到目前為止,代碼看起來像這樣......



    // 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,因此您不需要在標記中使用它。 然后,您可以只回顯變量或在必要時包含頁眉和頁腳。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM