duanhongxian6982 2011-11-23 18:03
浏览 76
已采纳

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

I now have a working JSON formatted file from my PHP scripts.

The next step is to have a JavaScript script to retrieve this data for sorting, filtering and displaying.

I have a working Ajax script that tests ok for pulling back data, but I need to personalize this to the individual.

Within PHP I have a Session variable called MID (Member ID).

I am trying to use PHP to build the JavaScript with the unique URL with the MID as a variable.

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.

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


       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);
       }
  • 写回答

1条回答 默认 最新

  • douren7921 2011-11-23 18:12
    关注

    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. Then you can just echo variables or include your header and footer where required.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退