如何通过一个JavaScript对象数组使用POST到PHP [英] How to pass a javascript object array to php using POST

查看:135
本文介绍了如何通过一个JavaScript对象数组使用POST到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有JavaScript对象的数组,我试图对这些对象传递给一个PHP页面将它们保存到数据库中。我没有问题传递一个变量到PHP,并使用该变量$ _ POST [项目],但我无法弄清楚如何通过对象的整个数组,这样我就可以访问我的objects.entryId和.mediaType值在PHP的页面。

呵呵,任何人问过,是我之所以要做这样说是因为我有一个Flash上​​传,你猜对了..上传到一个CDN服务器(远程)和远程服务器只回答后面这种JS对象。

感谢您的帮助,任何人都可以提供。

下面是我的JS功能:

 函数test(){
entriesObj1 =新的对象();
entriesObj1.entryId =ABC;
entriesObj1.mediaType = 2;
entriesObj2 =新的对象();
entriesObj2.entryId =高清;
entriesObj2.mediaType = 1;

VAR项=新的Array();

项[0] = entriesObj1;
项[1] = entriesObj2;
VAR parameterString;

对于(VAR I = 0; I< entries.length;我++){
parameterString + =(ⅰ大于0与&?:)
+测试+=
EN + codeURI(项[I] .entryId);
}

xmlhttp.open(POST,ajax_entries.php,真正的);

xmlhttp.setRequestHeader(内容型,应用程序/ x-WWW的形式urlen codeD);
xmlhttp.setRequestHeader(内容长度,parameterString.length);
xmlhttp.setRequestHeader(连接,关闭);

xmlhttp.onreadystatechange = handleServerResponseTest;
xmlhttp.send(parameterString);
}
功能handleServerResponseTest(){
如果(xmlhttp.readyState == 4){
如果(xmlhttp.status == 200){
警报(xmlhttp.responseText);
}
	     其他 {
警报(AJAX调用过程中出现错误,请重试。);
}
}
}
 

解决方案

也许你需要看看JSON和jQuery AJAX方法:

.- 的http://博客.reindel.com / 2007/10/02 /解析JSON的与 - jQuery的 - 和 - 的JavaScript /

.- http://us.php.net/json_de$c$c

该turorial也许是有点过时,因为jQuery的最后一个版本是1.3.x的,但你会得到一个想法上和对PHP的JSON的功能......如果你的服务器没有JSON的扩展激活,您可以使用一些PHP类:

.- <一href="http://google.com.co/search?rlz=1C1GPEA_enVE314VE314&sourceid=chrome&ie=UTF-8&q=php+json+class" rel="nofollow">http://google.com.co/search?rlz=1C1GPEA_enVE314VE314&sourceid=chrome&ie=UTF-8&q=php+json+class

祝你好运!

Lets say I have an array of javascript objects, and I am trying to pass those objects to a php page to save them into a database. I have no problems passing a variable to the php and using $_POST["entries"] on that variable but I can't figure out how to pass an entire array of objects, so I can access my objects.entryId and .mediaType values on the php page.

Oh and before anyone asks, yes the reason I need to do it this way is because I have a flash uploader, that you guessed it.. uploads into a CDN server (remote) and the remote server only replies back with such js objects.

Thanks for any help anyone can provide.

Here is my JS functions:

function test() {
		entriesObj1 = new Object();
		entriesObj1.entryId = "abc";
		entriesObj1.mediaType = 2;
		entriesObj2 = new Object();
		entriesObj2.entryId = "def";
		entriesObj2.mediaType = 1;

		var entries = new Array();

		entries[0] = entriesObj1;
		entries[1] = entriesObj2;
		var parameterString;

		for(var i = 0; i < entries.length; i++) {
			parameterString += (i > 0 ? "&" : "")
	          + "test" + "="
	          + encodeURI(entries[i].entryId);
		}

		xmlhttp.open("POST","ajax_entries.php",true);

		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", parameterString.length);
		xmlhttp.setRequestHeader("Connection", "close");

		xmlhttp.onreadystatechange  = handleServerResponseTest;
		xmlhttp.send(parameterString);
	}
	function handleServerResponseTest() {
	   if (xmlhttp.readyState == 4) {
	     if(xmlhttp.status == 200) {
		alert(xmlhttp.responseText);
	     }
	     else {
	        alert("Error during AJAX call. Please try again");
	     }
	   }
	}

解决方案

maybe you need to take a look at json and jQuery ajax methods:

.- http://blog.reindel.com/2007/10/02/parse-json-with-jquery-and-javascript/

.- http://us.php.net/json_decode

The turorial is maybe a little outdated because jQuery last version is 1.3.x but you will get an idea on that and about the PHP json functions... if your server does not have the json extension enabled you can use some php classes:

.- http://google.com.co/search?rlz=1C1GPEA_enVE314VE314&sourceid=chrome&ie=UTF-8&q=php+json+class

good luck!

这篇关于如何通过一个JavaScript对象数组使用POST到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆