We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10487 MODX Staff
    • 1,535 Posts

    1. How do I get the php file to work with MODX objects? I need to do a lot of DB processing.
    Use a MODX page to act as the ajax handler - all you need to do is use the blank template and put your code into a snippet on the page.
      Garry Nutting
      Senior Developer
      MODX, LLC

      Email: [email protected]
      Twitter: @garryn
      Web: modx.com
      • 32645
      • 377 Posts

      Use a MODX page to act as the ajax handler - all you need to do is use the blank template and put your code into a snippet on the page.

      I don’t understand how you make a MODX page act as an ajax handler.

      Lets see if I’ve got this right.

      1. Create a snippet with my php code in?

      <?php
      // [[exampleSnippet]] also known as combo.php
      $array = array();
      if ($_GET['_name'] == 'country')
      {
               if ( $_GET['_value'] == 3 )//usa
               {
                      $array[] = array('1' => 'Montana');
                      $array[] = array('2' => 'New York');
                      $array[] = array('3' => 'Texas');
                } else
                {
                      $array[] = array('0' => 'No state');
                }
      } elseif ($_GET['_name'] == 'state')
      {
               if ( $_GET['_value'] == 2 )//New York
               {
                      $array[] = array('1' => 'New York');
                      $array[] = array('2' => 'Another city');
                } else
                {
                      $array[] = array('0' => 'No city');
              }
      } else
      {
              $array[] = array('1' => 'Data 1');
              $array[] = array('2' => 'Data 2');
              $array[] = array('3' => 'Data 3');
      }
      echo json_encode( $array );
      ?>
      


      2. Create a page with my Javascript call, using the (blank) template

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
          <head>
              <title>jQuery ajax demo populating select dropdown menu</title>
      		<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
      		<script type="text/javascript" src="jquery.form.js"></script>
      		<script type="text/javascript" src="jquery.chainedSelects.js"></script>
      		<script type="text/javascript"> 
      		// wait for the DOM to be loaded 
      			$(function()
      			{
      				$('#country').chainSelect('#state','[b]combo.php[/b]',
      				{ 
      						before:function (target) //before request hide the target combobox and display the loading message
      						{ 
      								$("#loading").css("display","block");
      								$(target).css("display","none");
      						},
      						after:function (target) //after request show the target combobox and hide the loading message
      						{ 
      								$("#loading").css("display","none");
      								$(target).css("display","inline");
      						}
      				});
      				$('#state').chainSelect('#city','[b]combo.php[/b]',
      				{ 
      						before:function (target) 
      						{ 
      								$("#loading").css("display","block");
      								$(target).css("display","none");
      						},
      						after:function (target) 
      						{ 
      								$("#loading").css("display","none");
      								$(target).css("display","inline");
      						}
      				});
      
      			});
      		</script>
          </head>
      
      <style type="text/css">
      #loading
      {
      	display:none;
      	color:#CC3300;
      	border:2px solid #CC3300;
      	background:#FFCCCC;
      	font-weight:bold;
      	margin-bottom:1em;
      	/*
      	background:url("ajax-loader.gif");
      	background-repeat:no-repeat;
      	margin-bottom:1em;
      	height:16px;
      	*/
      }
      </style>
      <body>    
      
      <div id="loading">Loading</div>
      
      <form name="formname" method="post" action="">
              <!-- country combobox -->
              <select id="country" name="country">
      			<option value="1">France</option>
      			<option value="2">Romania</option>
      			<option value="3">Usa</option>
      			<option value="4">Brazil</option>
              </select>
              <!-- state combobox is chained by country combobox-->
              <select name="state" id="state" style="display:none"></select>
              <!-- city combobox is chained by state combobox-->
              <select name="city" id="city" style="display:none"></select>
      </form>
      
      </body>
      </html>
      


      What goes in the part where it says combo.php?

      I’ve tried putting [[sampleSnippet]] in this part and it does not work.

      Perhaps I’m making a mistake somewhere, if you can clarify this, that’d be great.
        • 32645
        • 377 Posts
        Okay, I’ve got it working (I think).

        1. Created page with the HTML posted above. Where it says combo.php I put [~80~], the ID of the page I want to grab the Ajaxed data from.
        2. Created the Ajaxed page (80) with a call to the snippet, which is a child of my existing page and is hidden and unpublished (so it won’t show in any sitemaps, or site searches).
        3. Created a snippet with the PHP code posted above.
        4. Launched the page.

        It worked.

        Now I’m going to do a whole solution with a connection to a DB and see if that works.
          • 16545
          • 358 Posts
          Your solution is for static data, not for dynamic one,
            • 32645
            • 377 Posts
            I know my solution posted above is for static data and not dynamic data.

            My problem/requirement is:

            1. User picks a country
            2. User picks a resort (a drop down generated by the choice of 1).

            I intend to program a solution which grabs the picked country ID and then query the DB to output a HTML drop down, the contents of which are the resorts linked to the country.

            If the solution I’ve posted is designed for static data, and there is a different solution for dynamic data, please post a solution for dynamic data so I might be able to use it.

            Thanks.
              • 11858
              • 130 Posts
              I’m trying to achieve the same thing, 3 levels deep in Revo - the thread is here: http://modxcms.com/forums/index.php?topic=58784.0

              Did you solve your problem worchyld?
                • 16681
                • 62 Posts
                Hi smiley
                Didnt read through the whole thread. I think my simplx_rpc Snippet will help you out with all your ajax needs. Try it out.

                Take a special look at the usage of getResources and how to get json results from it smiley

                /Lars
                  Keep it SIMPLX
                  • 18409
                  • 54 Posts
                  The solution in #6 won’t work because it doesn’t load the MODX parser etc. You are runnning entirely outside of MODX if you call the PHP directly.

                  See this thread: http://modxcms.com/forums/index.php/topic,60483.0.html
                  for details on how to do it, but essentialy you put the snippet call that returns the JSON/XML into a page with a blank template, and the front end JS request calls that page. This way your snippet runs inside of MODX.

                  I was hoping there might be a slightly neater way to do this that would enable calling the snippet directly without needed the intermediate page but there isn’t.
                    • 16681
                    • 62 Posts
                    Hi,
                    Just in case somebody stumble on to this in the future.
                    You can intercept ajax requests without using a dedicated template/page as host. Just make a plugin or use the terrific Simplx Controller package to catch the GET/POST request at the onHandleRequest event.

                    Ciao smiley
                      Keep it SIMPLX