来自PHP和输出缓冲的JavaScript(带有Ajax)(JavaScript (with Ajax) from PHP and Output Buffering)

编程入门 行业动态 更新时间:2024-10-26 23:34:08
来自PHP和输出缓冲的JavaScript(带有Ajax)(JavaScript (with Ajax) from PHP and Output Buffering)

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

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

我有一个工作的Ajax脚本,可以测试回退数据,但我需要将其个性化为个人。

在PHP中,我有一个称为MID(成员ID)的Session变量。

我正在尝试使用PHP来构建带有MID作为变量的唯一URL的JavaScript。

除了用外部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); }

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); }

最满意答案

我觉得像这样简单的东西应该适合你。

<?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,因此您不需要它在标签中。 然后,您可以在需要的地方回显变量或包含页眉和页脚。

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.

更多推荐

本文发布于:2023-04-27 13:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1326155.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JavaScript   PHP   Ajax   Buffering   Output

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!