We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32443
    • 37 Posts
    I would like to design a plugin or snippet (i don’t know which one it would be as i am new to the modx developer community) that would take a get or post header and return the xml inside of the id of a div element i chose

    so it would either be a get www.example.com/index.html?getxml=testdiv or a post of the same type

    i want to do this so i can use ajax to get a portion of a page but still retain whole pages for better search engine optimization and compatibilty when users have javascript disabled.

    so would someone please help me figure out how to go this?
      call it ModSeX o yeah smiley

      check us out and we need help Upscale Webservices http://www.upscalews.com/
      • 32443
      • 37 Posts
      i figured it out if anyone would like to know how just ask smiley
        call it ModSeX o yeah smiley

        check us out and we need help Upscale Webservices http://www.upscalews.com/
        • 25663 MODX Staff
        • 12,272 Posts
        Go ahead and share, please. smiley
          Ryan Thrash, MODX Co-Founder
          Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
          • 32443
          • 37 Posts
          sorry so long to answer i just got back into the swing of things from christmas break smiley

          ok it works like this

          ummm first you need the DOMadapter class from http://www.phpclasses.org/browse/file/16454.html

          this makes it so the DOM walking will work in either php 4 or 5 d-(^_^)-b

          then you use this

          <?php
          /*
          
          * William Harris
          * Get Document Xml By Id
          *
          * Kinda Straightforward :)
          
          // Copyright (c) 2007 William Harris (http://upscalews.com, http://williamharris.name)
          //
          // Permission is hereby granted, free of charge, to any person obtaining
          // a copy of this software and associated documentation files (the
          // "Software"), to deal in the Software without restriction, including
          // without limitation the rights to use, copy, modify, merge, publish,
          // distribute, sublicense, and/or sell copies of the Software, and to
          // permit persons to whom the Software is furnished to do so, subject to
          // the following conditions:
          //
          //  just Credit Me Please and perhaps link to our site I would like that :)
          //
          // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
          // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
          // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
          // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
          // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
          // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
          // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
          *
          * and if you could visit our site www.upscalews.com  we are a company by the name 
          * of Upscale Webservices we make websites if you need one designed
          */ 
          
          /*
          * function that parses all the way through the xml of a document (or a variable with xml in it as in this case) 
          * and gets all of it
          */
          
          function nodeContent($node)
          {
             $str = "";
             if ($node->hasChildNodes())
             {
                 foreach($node->childNodes as $child)
                 {
                     $str .= $child->ownerDocument->saveXML($child);
                 }
             }
             return $str;
          }
          
          
          // include Dom Adapter for back and forwards compatibility  -- so beautiful :_(
          //require_once($_SERVER['DOCUMENT_ROOT'] . "/myinc/DOMAdapter.inc.php");
          require_once("DOMAdapter.inc.php");
          
          
          // header to serve as xml for production or plain text for testing
          //header("origContent-Type: text/plain");
          header("origContent-Type: text/xml");
          
          
          // -------------- Begin Input XMl Logic -----------------//
          // create input document object
          $inxml = new DOMAdapter();
          
          // make reference to xml
          $indoc = $inxml->createDocument();
          
          // pick page to get from
          //$wpage = $_SERVER['DOCUMENT_ROOT'] . '/hustle/' ."index.html" ;
          //$wpage = '../dir/index.html';
          //$wpage = '../dir/' . $_POST['page'] . '.html';
          $wpage = $_POST['dir'] . $_POST['page'] . '.html';
          
          //More Testing
          //var_dump($_POST['page']);
          //var_dump($_POST['elId']);
          //var_dump($_POST['dir']);
          //var_dump($_POST['newEl']);
          
          // load xml
          $indoc->validateOnParse = true;
          //$indoc->load($wpage);
          $indoc->loadHTMLFile($wpage);
          
          //var_dump($wpage);
          
          //chose the id you wish to get from the document
          //$elId = "page";
          $elId = $_POST['elId'];
          $newEl = $_POST['newEl'];
          
          $selectEl = $indoc->getElementById($elId);
          // -------------- End Input XMl Logic  -----------------//
          
          
          // make a string with the div origContent because you can't add xml from one source to another... no idea why 
          //$origContent = '<div id="page">' . nodeContent($selectEl) . "</div>";
          $origContent = '<div id="'. $elId .'">' . nodeContent($selectEl) . "</div>";
          
          if ( $newEl != NULL ) {
          	$newContent = '<div id="'. $newEl .'">' . nodeContent($selectEl) . "</div>";
          	$outContent = $newContent;
          } else {
          	$outContent = $origContent;
          }
          
          // kind of element you want just in case you need that
          //$kind = "div";
          //$origContent = '<'. $kind . ' id="'. $elId .'">' . nodeContent($selectEl) . '</'. $kind .'>';
          
          
          // a debuging type of thing var_dump($origContent);
          
          
          // -------------- OUtupt XMl Logic Begin -----------------//
          
          // create output document object
          $outxml = new DOMAdapter();
          
          // make reference to xml and form it
          $outdoc = $outxml->createDocument();
          //$outdoc->documentElement->setAttribute("id", $elId);
          
          // load in Xml
          $outdoc->loadXML($outContent);
          
          // "print" it
          echo $outdoc->saveXML();
          
          // -------------- Output XMl Logic End -----------------//
          
          ?>
          
          


          its ok but i would like some feedback (im a php n00b so any help would be appreciated) and i don’t think the domAdapter works right on php4

          the feed back i would like is because modx uses voodoo (read: modRewrite) to generate the pages in a site and all my humble function does is crawl a variable full of xml (or (x)html in this case) for a node of a certain id and return it. So in order for it work with modx you must use [glow=red,2,300]http[/glow] to get the html, like this:

          // it's a post
          &dir=http://yoursite.com/wanteddirectory/&page=desiredpage&elId=desireddocumentid
          
          // if you want your output to have a different id
          &dir=http://yoursite.com/wanteddirectory/&page=desiredpage&elId=desireddocumentid&newEl=newdocumentid
          


          instead of any kind of relative link you like

          and also would like to make it a modx plugin or snippet (again im new so i don’t even know which one it would be)

          i know for a fact that it works because i have been using it on my test server ( php5 http://uniformserver.com for anyone interest in a wamp sort of thing ) on my pc for weeks

          thanks for you time smiley d-(^_^)-b
            call it ModSeX o yeah smiley

            check us out and we need help Upscale Webservices http://www.upscalews.com/
            • 33372
            • 1,611 Posts
            So let me see if I understand what you’re doing correctly...

            You have a page (let’s call it the master page) made up of several divs with unique IDs. You output them all together on this page. But you also output individual div content only on other pages.

            Is that correct?

            If so, it seems to me that a little bit of restructuring would make this problem a non-issue. If you made your master page a folder and each of your divs a separate document inside it, then you could use Ditto or a simple custom snippet to output the master page with the div content combined. And of course the individual divs would also be accessible on their own with no extra work.

            Assuming that’s what you needed done, this is only one way of doing it using the MODx structure and API. I bet that the folks here could come up with at least half a dozen more within 72 hours (now that’s a challenge). On your marks, get set...
              "Things are not what they appear to be; nor are they otherwise." - Buddha

              "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

              Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
              • 32443
              • 37 Posts
              Quote from: ZAP at Jan 16, 2007, 03:15 PM

              So let me see if I understand what you’re doing correctly...

              You have a page (let’s call it the master page) made up of several divs with unique IDs. You output them all together on this page. But you also output individual div content only on other pages.

              Is that correct?

              If so, it seems to me that a little bit of restructuring would make this problem a non-issue. If you made your master page a folder and each of your divs a separate document inside it, then you could use Ditto or a simple custom snippet to output the master page with the div content combined. And of course the individual divs would also be accessible on their own with no extra work.

              Assuming that’s what you needed done, this is only one way of doing it using the MODx structure and API. I bet that the folks here could come up with at least half a dozen more within 72 hours (now that’s a challenge). On your marks, get set...

              thats getting there

              I have several pages where everything is exactly the same (through templating) except whats in a certain div with the id page


              div id = "page"


              that div contains the contents of that particular page and that is all that changes on any given page

              i want to be able to serve just that div to an ajax call so that I can update any page with any other page’s content

              and I hope that explains it
                call it ModSeX o yeah smiley

                check us out and we need help Upscale Webservices http://www.upscalews.com/
                • 33372
                • 1,611 Posts
                Er...wait...

                So all you need is to be able to put the page content from these documents (the text inside your page div) on other pages with a different template?!?

                If that’s the case, then this is REALLY easy to do using MODx’s API...
                  "Things are not what they appear to be; nor are they otherwise." - Buddha

                  "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                  Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  AJAX. That means an external .php file/function. That means the $modx object is not going to be available, unless you want to include both the /manager/includes/config.inc.php files and the /manager/includes/document.parser.inc.php files and instantiate a new $modx object. To do that, see the main index.php file.

                  It would actually be almost as easy and less overhead to simply include the /manager/includes/config.inc.php file, then make your own query to the database "site_content" table to get the content field. Then you can do with it as you please.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 33372
                    • 1,611 Posts
                    OK maybe I’m not getting this question at all, but I don’t think that AJAX is a problem here. If he wants to use the XML files that he creates from this document content as part of an AJAX system, how does it matter that MODx generated the XML? So far we’re just looking at how to create the XML files that could be used for AJAX (or RSS, or whatever); in the next step he may need to worry about how to put it all together.
                      "Things are not what they appear to be; nor are they otherwise." - Buddha

                      "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                      Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                      • 32443
                      • 37 Posts
                      ok I believe we are on the same page so

                      in that case how would use the api to go through the database and then serve the content

                      (the portion of the page thats says div id = page) as xml for my ajax call then?

                      and i am searching and going through and learning I am just asking to be thorough

                      Quote from: ZAP at Jan 17, 2007, 12:07 AM

                      OK maybe I’m not getting this question at all, but I don’t think that AJAX is a problem here. If he wants to use the XML files that he creates from this document content as part of an AJAX system, how does it matter that MODx generated the XML? So far we’re just looking at how to create the XML files that could be used for AJAX (or RSS, or whatever); in the next step he may need to worry about how to put it all together.

                      i got the ajax down and how to put it all together

                      i keep everything else on the page exactly the same except the xhtml inside the div with the id page

                      so when i make the Ajax call I just put all the content inside the div that contains the div with the id page

                      i just need to be able to get the xml to update the page with

                      and as sottwell said if I used an outside php filei wouldn’t have access [wouldn’t know how to access? ] the modx object

                      so all i need is something to serve me the xml as xml
                        call it ModSeX o yeah smiley

                        check us out and we need help Upscale Webservices http://www.upscalews.com/