meomike2000 Posted February 6, 2010 Share Posted February 6, 2010 if i were to post an xml document from an ajax script to my php script for processing how do i get/catch the xml in my php script. i have learned how to parse the xml in php by creating it then pasing the results all in same php script. so i can handle that part with this code, but how do i get the value of $xmldoc, see line 3&4 of code below for what $xmldoc is suppost to be , that is the problem i got. //create new dom document $xml = new DOMDocument(); //get sent xml $xmldoc = ?????; //load xmldoc $xml->loadXML($xmldoc); // statement block to get at the elements of the xml document $friendxml = $xml->getElementsByTagName('friend'); foreach ($friendxml AS $f) { foreach ($f->childNodes AS $item) { // print $item->nodeName . " = " . $item->nodeValue . "<br />"; } //print "<br />"; } hope this makes sense, hope you can help, thanks in advance mike. Quote Link to comment https://forums.phpfreaks.com/topic/191185-xmlphp-question/ Share on other sites More sharing options...
MadTechie Posted February 7, 2010 Share Posted February 7, 2010 You say the XML is being set from AJAX, so the $xmldoc would be set from request that ajax is sending. (ie $_GET['ajax_request']) Quote Link to comment https://forums.phpfreaks.com/topic/191185-xmlphp-question/#findComment-1008130 Share on other sites More sharing options...
meomike2000 Posted February 7, 2010 Author Share Posted February 7, 2010 may need to move this to the ajax board, not sure..... i tried this $xmldoc=$_GET['ajax_request']; $xml = new DOMDocument(); $xml->loadXML($xmldoc); but that dont seem to work, i will play with it some, maybe i am doing something wrong in the posting part. here is the segment that does the posting..... var ajax = new Ajax(); var postData = self.xml; ajax.doPost('../scripts/friends/updateFriendsOrder.php?uid=' + uid + '&date=' + date, postData, self.handleSave, 'xml'); Quote Link to comment https://forums.phpfreaks.com/topic/191185-xmlphp-question/#findComment-1008174 Share on other sites More sharing options...
MadTechie Posted February 7, 2010 Share Posted February 7, 2010 okay.. without knowing more about the doPost function its kinda hard to say what that call does!.. so can you post that script or atleast what variable is being set Quote Link to comment https://forums.phpfreaks.com/topic/191185-xmlphp-question/#findComment-1008180 Share on other sites More sharing options...
meomike2000 Posted February 7, 2010 Author Share Posted February 7, 2010 here is the complete ajax script.... function Ajax() { this.req = null; this.url = null; this.method = 'GET'; this.async = true; this.status = null; this.statusText = ''; this.postData = null; this.readyState = null; this.responseText = null; this.responseXML = null; this.handleResp = null; this.responseFormat = 'text'; // 'text', 'xml', or 'object' this.mimeType = null; this.init = function() { if (!this.req) { try { //Try to create object for Firefox, Safari, IE7, etc. this.req = new XMLHttpRequest(); //alert("Your browser is firefox!"); } catch (e) { try { // Try to create object for later versions of IE. this.req = new ActiveXObject("Msxml2.XMLHTTP"); //alert("Your browser is a later version"); } catch (e) { try { //Try to create object for early versions of IE. this.req = new AxtiveXObject("Microsoft.XMLHTTP"); //alert("Your browser is a older version!"); } catch (e) { //could not create an xmlhttprequest object. alert("Your browser broke!"); return false; } } } } return this.req; }; this.doReq = function() { if (!this.init()) { alert('could not create XMLHttpRequest object.'); return; } this.req.open(this.method, this.url, this.async); if (this.method == "POST") { this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); } if (this.mimeType) { try { req.overrideMimeType(this.mimeType); } catch (e) { // couldn't override MIME type -- IE6 or Opera? } } var self = this; // Fix loss-of-scope in inner function this.req.onreadystatechange = function() { var resp = null; if (self.req.readyState == 4) { switch (self.responseFormat) { case 'text': resp = self.req.responseText; break; case 'xml': resp = self.req.responseXML; break; case 'object': resp = req; break; } if (self.req.status >= 200 && self.req.status <= 299) { self.handleResp(resp); } else{ self.handleErr(resp); } } }; this.req.send(this.postData); }; this.setMimeType = function(mimeType) { this.mimeType = mimeType; }; this.handleErr = function() { var errorWin; try { errorWin = window.open('', 'errorWin'); errorWin.document.body.innerHTML = this.responseText; } catch (e) { alert('An error occurred, but the error message cannot be ' + 'displayed. This is probably because of your browser\'s ' + 'pop-up blocker.\n' + 'Please allow pop-ups from this web site if you want to ' + 'see the full error message.\n' + '\n' + 'Status Code: ' + this.req.status + '\n' + 'Status Description: ' + this.req.statusText + ' ' + this.url); } }; this.abort = function() { if (this.req) { this.req.onreadystatechange = function() { }; this.req.abort(); this.req = null; } }; this.doPost = function(url, postData, hand, format) { this.url = url; this.handleResp = hand; this.responseFormat = format || 'text'; this.method = 'POST'; this.postData = postData; this.doReq(); }; this.doGet = function(url, hand, format) { this.url = url; this.handleResp = hand; this.responseFormat = format || 'text'; this.doReq(); }; } Quote Link to comment https://forums.phpfreaks.com/topic/191185-xmlphp-question/#findComment-1008236 Share on other sites More sharing options...
MadTechie Posted February 7, 2010 Share Posted February 7, 2010 okay.. lets try this a different way.. what are you trying to do, as i fail to see the reason for sending an xmlfile php via ajax (the truth of the matter is that your not sending the file), why not just send a request with the filename and have PHP pick the name and read it in. Quote Link to comment https://forums.phpfreaks.com/topic/191185-xmlphp-question/#findComment-1008254 Share on other sites More sharing options...
meomike2000 Posted February 7, 2010 Author Share Posted February 7, 2010 the original file is created with php and then sent to the ajax script, there a user can update the things and then save. on save i need to send the modified xml back to php for processing/saving. i do not use actual xml files. all the info comes from database, so i need to send it back to be saved in database. i need this to be returned as xml, as some users may only need to send back one or two things to be updated while others may need to send back 10 20 or more items to be updated..... Quote Link to comment https://forums.phpfreaks.com/topic/191185-xmlphp-question/#findComment-1008256 Share on other sites More sharing options...
meomike2000 Posted February 7, 2010 Author Share Posted February 7, 2010 here is a sample of my idea, in this example php file, the info is gotten out the database and formated into xml then it is loaded into a domdocument and parsed..... this is much the same idea as to what i am trying to do except the xml is requested by ajax, this lets the user make changes, then i need to be able to send this xml back to the server/php so the changes can be saved... http://www.meo2000.net/web/getallfriends.php Quote Link to comment https://forums.phpfreaks.com/topic/191185-xmlphp-question/#findComment-1008261 Share on other sites More sharing options...
meomike2000 Posted February 8, 2010 Author Share Posted February 8, 2010 i have found that i needed to add/make some changes/ to my ajax script..... not sure if this is correct or not but seems correct.... made this change to this.doReq = function() if (this.method == "POST") { //here is the change..... if (this.responseFormat == "xml") { this.req.setRequestHeader("Content-Type", "text/xml"); }else{ //sending plain text this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); } } Quote Link to comment https://forums.phpfreaks.com/topic/191185-xmlphp-question/#findComment-1008633 Share on other sites More sharing options...
MadTechie Posted February 8, 2010 Share Posted February 8, 2010 I'm sorry but I don't understand what your trying to do as your questions don't seam to fit with your logic and your logic has holes! for example where does the XML get generated ? what parameters are sent from ajax ? Quote Link to comment https://forums.phpfreaks.com/topic/191185-xmlphp-question/#findComment-1008649 Share on other sites More sharing options...
meomike2000 Posted February 8, 2010 Author Share Posted February 8, 2010 the xml is generated from a php script, here is a example of the xml with out info filled in, http://www.meo2000.net/web/getallfriends.php the entire user page is generated with js/ajax and i use the AJAX doGET(); to get the xml, then it is parsed and displayed to the user from js/ajax. the user can make changes to the information, some or all, and then the user can save the changes. when the user makes the changes it changes the xml that is stored in a js/ajax global array this/self.xml. i need to send that xml back to the server to be parsed and saved. look at the example xml, that is just part of the info, how else could i send the xml back...... is this possible, and if so, how do i send xml back to the server from my ajax script to my php script...... sorry if it seams confusing.... mike.. Quote Link to comment https://forums.phpfreaks.com/topic/191185-xmlphp-question/#findComment-1008651 Share on other sites More sharing options...
MadTechie Posted February 8, 2010 Share Posted February 8, 2010 the xml is generated from a php script, here is a example of the xml with out info filled in, http://www.meo2000.net/web/getallfriends.php Let me stop you their, That's NOT XML, its barely a snippet of html what parameters are sent from ajax ? Assuming self.xml is a string, can't you just use var postData = "xml="+self.xml; then pick up the $_POST['xml'] form the PHP side Quote Link to comment https://forums.phpfreaks.com/topic/191185-xmlphp-question/#findComment-1008659 Share on other sites More sharing options...
meomike2000 Posted February 8, 2010 Author Share Posted February 8, 2010 i have tried to just post it and pick it up, no luck. here is the complete php file that generates the xml... done to standards..... here i parsed it and printed the results for u. in the real file i dont i send it with the xml header and print the saved xml. those are commented out for the view, i will change that so it is xml for u to see..... look now same file but changed to display with header xml http://www.meo2000.net/web/getallfriends.php also, in my real life scripts i have set the header to xml so yes it is true xml.... <?php include 'conf.php'; $userid = 1; $query = null; //open connection to db. $connection = mysql_connect($host, $user, $pass) or die ('unable to connect!'); //select database to use. mysql_select_db($db) or die ('unable to select database!'); //$query = "select friendid from friends where myid = '" . $userid . "' and status = 'accepted'"; $query = "select users.scrname, users.lastlog, upage.inf, upage.logtime, friends.friendid from users, upage, friends where users.uid = friends.friendid and upage.fk_uid = friends.friendid and friends.myid = '".$userid."' and friends.status = 'accepted'"; $result = mysql_query($query); $query = null; $dom = new DOMDocument('1.0', 'iso-8859-1'); $friends = $dom->createElement('friends'); $dom->appendChild($friends); while ($row = mysql_fetch_row($result)) { $id = $row[4]; if ($row[0] == "double click to set name") { $query = "select uname from users where uid = '".$row[4]."'"; $results = mysql_query($query); $rows = mysql_fetch_row($results); mysql_free_result($results); $uname = $rows[0]; }else { $uname = $row[0]; } $date = get_time($row[1]); //createthumbnail($row[2]); $pic = $row[2]; $uname = htmlentities($uname); $friend = $dom->createElement('friend'); $friends->appendChild($friend); $fid = $friend->appendChild($dom->createElement('id')); $fid->appendChild($dom->createTextNode("id: $id")); $fname = $friend->appendChild($dom->createElement('name')); $fname->appendChild($dom->createTextNode("name: $id")); $fpic = $friend->appendChild($dom->createElement('pic')); $fpic->appendChild($dom->createTextNode("pic: $id")); } $xmldoc = $dom->saveXML(); mysql_free_result($result); //close database mysql_close($connection); $query = null; //echo $xmldoc; //$xml = new DOMDocument(); //$xml->loadXML($xmldoc); // statement block to get at the elements of the xml document //$friendxml = $xml->getElementsByTagName('friend'); //foreach ($friendxml AS $f) { // foreach ($f->childNodes AS $item) { // print $item->nodeValue . "<br />"; // } // print "<br />"; //} header("Content-Type: text/xml") echo $xmldoc; ?> Quote Link to comment https://forums.phpfreaks.com/topic/191185-xmlphp-question/#findComment-1008664 Share on other sites More sharing options...
meomike2000 Posted February 8, 2010 Author Share Posted February 8, 2010 self.xml is an array that stores the xml...... i tried to store it as a string as u suggest that this is not xml and the script fails....... Quote Link to comment https://forums.phpfreaks.com/topic/191185-xmlphp-question/#findComment-1008670 Share on other sites More sharing options...
meomike2000 Posted February 9, 2010 Author Share Posted February 9, 2010 well my solution was to add the row id to the xml then after changes where made i loop through the xml and add the rowid to the changed value with a seperator and then add that to my post array and then i send that to the php script where i loop through my $_POST[] array and then split the value to get the rowid to update with the new value.... while this works i still would like to find a way to post xml back to a php script....... here is the changes to my ajax that adds the post array. and then sends it to php.... also i still get my response back from the php script after processing as xml.... this.setPost = function() { var self = friendspage; var res = []; res = self.xml.getElementsByTagName("friend"); var form = document.createElement('form'); //var inputs = []; for (var a = 0; a < res.length; a++) { self.xmlfriend[a] = res[a]; var rowid = res[a].getElementsByTagName("rowid")[0].childNodes[0].nodeValue; var input = document.createElement('input'); input.type = 'hidden'; input.name = 'input[' + a + ']'; input.value = rowid; form.appendChild(input); } self.doSave(form); }; this.doSave = function(form) { var self = friendspage; var uid = self.uid; var date = new Date(); var ajax = new Ajax(); var postData = formData2QueryString(form); ajax.doPost('../scripts/friends/updateFriendsOrder.php?uid=' + uid + '&date=' + date, postData, self.handleSavexml, 'xml'); }; here is the php file that does the updateing... <?php include '../../conf.php'; $userid = $_GET['uid']; $query = null; //open connection to db. $connection = mysql_connect($host, $user, $pass) or die ('unable to connect!'); //select database to use. mysql_select_db($db) or die ('unable to select database!'); $a = 0; foreach ($_POST['input'] as $fid) { //$fid = $_POST["input[$a]"]; $query = "update friends set orderid = '".$a."' where fid = '".$fid."'"; $updateorder = mysql_query($query); $query = null; $a++; } header("Content-Type: text/xml"); print '<?xml version="1.0" encoding="utf-8" ?> <response>'; if ($a == 0) { print '<type>order</type> <status>fail</status> <a>'.$a.'</a>'; }else{ print '<type>order</type> <status>success</status> <a>'.$a.'</a>'; } print '</response>'; ?> hope this may help anybody that is trying to do something similar.... mike Quote Link to comment https://forums.phpfreaks.com/topic/191185-xmlphp-question/#findComment-1009219 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.