在没有JSON的情况下将数组从PHP返回到JavaScript

时间:2022-10-24 14:11:13

I'm trying to finish a school project but can't seem to find any solutions. I need to access a database, format the rows into an array, and then return it to a JavaScript file to be used to create a graphic using the Google Charts API.

我正在努力完成一个学校项目,但似乎无法找到任何解决方案。我需要访问数据库,将行格式化为数组,然后将其返回到JavaScript文件,以使用Google Charts API创建图形。

I can't use PHP since I have to use the school's PHP server which is running PHP 5.1.6. I saw some code that allows you to use JSON on earlier versions of PHP but I'm not sure how to actually add that to my PHP file.

我不能使用PHP,因为我必须使用运行PHP 5.1.6的学校的PHP服务器。我看到一些代码允许您在早期版本的PHP上使用JSON,但我不确定如何将其实际添加到我的PHP文件中。

Any suggestions?

有什么建议么?

1 个解决方案

#1


1  

Just print out the list of values separated by comma, then use split in Javascript:

只需打印出以逗号分隔的值列表,然后在Javascript中使用split:

var returnVars = xmlhttp.responseText.split(",");
for(var i=0; i<returnVars.length; i++)
{
     alert(returnVars[i]);
}

This is assuming an Ajax request. If you're just writing the Javascript into the PHP, obviously its as simple as making the PHP code write out what the Javascript for an array would look like.

这是假设一个Ajax请求。如果您只是将Javascript写入PHP,显然它就像让PHP代码写出数组的Javascript看起来一样简单。

#1


1  

Just print out the list of values separated by comma, then use split in Javascript:

只需打印出以逗号分隔的值列表,然后在Javascript中使用split:

var returnVars = xmlhttp.responseText.split(",");
for(var i=0; i<returnVars.length; i++)
{
     alert(returnVars[i]);
}

This is assuming an Ajax request. If you're just writing the Javascript into the PHP, obviously its as simple as making the PHP code write out what the Javascript for an array would look like.

这是假设一个Ajax请求。如果您只是将Javascript写入PHP,显然它就像让PHP代码写出数组的Javascript看起来一样简单。