Re: Parsing AJAX post data -- The Way

From: Date: Thu, 25 Jan 2007 09:46:30 +0000
Subject: Re: Parsing AJAX post data -- The Way
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-247774@lists.php.net to get a copy of this message
hi The "X" in AJAX says that the data are XML Data. To parse XML I use DOM. It is also possible to validate the data using RelaxNG, DTD or XMLSchema before processing. A receiver in PHP looks like <?php //read the string ($HTTP_RAW_POST_DATA is by default //not active -> use "php://input") $content=file_get_contents("php://input"); //make a dom object //many people know DOM already from programming javascript $dom=domDocument::loadXML($content); //validate with DTD,RelaxNG or XMLSchema //http://at.php.net/manual/de/function.dom-domdocument-relaxngvalidate.php if($dom->relaxNGValidate("test.rng")){ //than read the data you need //for example if your XML looks like //<person born="1935"> //<firstname>Dalei</firstname><lastname>Lama</lastname> //</person> $firstname=$dom->getElementsByTagName('firstname')->item(0); $lastname=$dom->getElementsByTagName('lastname')->item(0); $born=$dom->documentElement->getAttribute('born'); }else{ echo "data are not valid"; } ?> If you don't like XML (AJAX) use JSON. Bernhard Am Donnerstag, den 25.01.2007, 00:00 -0700 schrieb M5: > Just wondering what smart people do for parsing data sent by the > Javascript XMLHTTP object--e.g., http.send("post",url,true)... > > In a normal form submit, the $_POST global nicely allocates form > elements as array elements automatically. But with the AJAX way, the > data get stuffed inside $HTTP_RAW_POST_DATA as a string, thereby > making extraction more tedious. > > Any ideas? > > ...Rene >

« previous php.general (#247774) next »