JavaScript(带Ajax)来自PHP和输出缓冲
JavaScript (with Ajax) from PHP and Output Buffering
我现在有一个工作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,所以您不需要在标签中。然后,你可以只是回显变量或在需要的地方包含页眉和页脚。
相关文章:
- 使用PHP输出JavaScript时会出现神秘的换行符
- 将 PHP 输出到 JavaScript 时出现语法错误
- AngularJS:在ng-reeat中从php输出数组(带键)
- 将 PHP 输出 (AJAX) 分配给 JavaScript 变量
- 使用 PHP 输出的 Javascript 数组时出现未定义错误
- 将 PHP 输出解析为 javascript 的变量
- 使用 AJAX 从数组中读取 PHP 输出
- PHP 输出到滚动选框
- 刷新 php 输出而不重新加载页面
- 在表中使用 JavaScript 获取 PHP 输出
- Jquery/php 输出自定义错误消息
- 使用 AJAX 和 PHP 输出 PDF
- 使用 CloudFlare 时 PHP 输出的额外字符
- PHP输出到JS以呈现为HTML
- Php输出破坏了Javascript
- SyntaxError:JSON.parse:解析PHP输出时出现意外字符
- 通过select2-x-editable获取服务器端php输出
- 让php输出带引号的Json,以便javascript将其视为一个对象
- php输出到带有json的javascript数组
- DOM与PHP输出不匹配