将Javascript与php结合使用
Using Javascript with php
我知道这个问题已经被问过几次了,但我正在尝试将javascript与php结合使用。我有一个名为parsing.php的文件,它通过xml提要进行解析,并将元数据转换为名为"data"的JSON对象。解析是使用带有JavaScript和JQuery的ajax调用来完成的。
<script src="json2.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript">
$.ajax({
type: 'GET',
url: 'fakeFeed.xml',
dataType: 'xml',
async: false,
success: function(data, textStatus, jqXHR) {
function getRandom(max) {
return Math.floor(Math.random() * max);
}
function getThumbId(small) {
var num = getRandom(15);
if (num == 0) {
num = 1;
}
if (num < 10) {
num = '0' + num;
}
return num.toString();
}
var categories = new Array(); // Array for the categories
var category = {
name : '',
videos: []
};
var data1 = data;
var data = {
categories: []
};
$(data1).find('item').each(function () {
var el = $(this);
var categoryName = el.find('category').text();
var p = categories.indexOf(categoryName);
if( p == -1) {
categories.push(categoryName);
var category = {
name: categoryName,
videos: []
};
for (var j = 0; j<5; j++) {
var video = {
sources: [el.find('media'':content, content').attr('url')],
thumb : 'images'/thumbs'/thumb' + getThumbId() + '.jpg',
title : el.find("title").text(),
subtitle : el.find("description").text(),
description: ""
}
category.videos.push(video);
}
data.categories.push(category);
}
});
window.data = JSON.stringify(data);
<script>
"<?php
$dataVar = ?> <script type=text/javascript>window.data</script><?php;?>"
"<?php
print_r($dataVar,true);
?>"
我需要使用javascript和php的唯一原因是,我想使用php中的"print_r()"函数,它允许我返回信息,而不仅仅是将信息打印到屏幕上,但不幸的是,我无法使其工作。如果有人知道其他选择,或者能提供一些建议,我们将不胜感激。
以下是我相信您正在努力实现的目标,用PHP编写:
$dom = new DOMDocument();
$dom->load("fakeFeed.xml");
$data = ["categories"=>[]]; // may need to use array() instead of [] depending on PHP version
foreach($dom->getElementsByTagName('item') as $item) {
$name = trim($item->getElementsByTagName('category')->item(0)->textContent);
if( !isset($data['categories'][$name])) {
$cat = ["name"=>$name,"videos"=>[]]; // again, adjust if you're on an older version
// I'm not entirely sure what you're trying to achieve on this part.
// you seem to be getting the same video five times...
// revise your approach, comment if needed, and I can try to help
// for now, "insert code here"
$data['categories'][$name] = $cat;
}
}
// we were using the name as a key for simplicity, now just take the values
$data['categories'] = array_values($data['categories']);
// done! $data now has your data.
var_dump($data);
如果您真的想使用这个,而不是对JS:使用document.log
$.ajax({
type: "POST",
url: "some_php.php",
data: JSON.stringify(data);
})
.done(function( msg ) {
document.write(msg);
});
和some_php.php
$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
相关文章:
- 正确地将javascript与php结合起来
- 将post方法与php、ajax和javascript结合使用
- 将Javascript与php结合使用
- 如何将jQueryAJAX与PHP结合使用,使用<a>标签
- 如何将PHP的DOMDocument与JavaScript模板相结合
- 如何将简单的 JQuery 自动完成语法与对远程数据源(PHP 脚本)的 ajax 调用相结合
- Node.js与传统PHP框架:如何结合
- PHP在javascript游戏开发中的结合
- 动态内容在结合jQuery和PHP时不会不更新
- Jquery getScript 或 ajax 与 php flush 相结合
- 将 jQuery 点击处理程序与同一按钮上的 PHP 标头相结合
- 是否可以将ImageNow与Javascript和php相结合
- PHP和JS相结合
- 将php文件与AJAX结合使用的最佳实践
- PHP + JQuery,如何在函数中结合使用这两者
- 表单与jquery结合PHP发送结果*更新完整的代码*
- 结合vejs v-repeat和php foreach循环
- Web应用:结合PHP和Javascript-AJAX之间的视图层
- 结合PHP和jQuery循环图片,匹配id,并在幻灯片中输出图像
- 如何在分页控件中结合PHP和jQuery