We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16545
    • 358 Posts
    Is it possible to make smth. like this http://satya61229.blogspot.com/2007/04/dynamically-populate-select-list-by.html using document directory structure.

    Suggestions, solutions or just an ideas will not be missed.
    Thx.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Of course, just do it like they did. It would be tricky to get to work with eForm, though.
        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
        • 16545
        • 358 Posts
        Maybe somebody have an working snippet, which using this stuff?

        Quote from: sottwell at Jul 21, 2008, 01:13 AM

        Of course, just do it like they did. It would be tricky to get to work with eForm, though.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          It’s AJAX. Javascript, not a snippet. The only php is in the backend script, which in this case has nothing to do with MODx.
            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
            • 16545
            • 358 Posts
            Understood, but you wanna say it is not impossible to make flexible contruction within TV or just related documents, which could be automatically generated by Ditto or another snippet or plugin?

            Quote from: sottwell at Jul 21, 2008, 01:51 AM

            It’s AJAX. Javascript, not a snippet. The only php is in the backend script, which in this case has nothing to do with MODx.
              • 29774
              • 386 Posts
              Ditto can be invoked with ajax, indirectly - jQuery’s + the jQuery form plugin would be a good choice for this, you can use it to invoke a custom snippet that returns a JSON encoded string generated from a Ditto results resource. Each city would be represented represented as a modx document.
              The js
              $('#country').change(function() { 
               	var options={
                		url:			'/assets/snippets/myCustomSnippet.php',
              		dataType:		'json',
              		success:		updateForm //post-submit callback
               	};
               	// submit the form 
               	$('#myForm').ajaxSubmit(options);
              });
              
              function updateForm(data, statusText) {
              	//do stuff with returned data, like populate a select menu with cities 
              }
              


              myCustomSnippet.php
              $country = isset($_POST['country'])? intval($_POST['country']) : '0';
              $modx->runSnippet('Ditto', 
              	array(			
              		"parents"    =>$country,
              		"depth"      =>1,
              		"display"    =>"all",
              		"save"       =>"2",
              		"tpl"        =>"@CODE [+pagetitle+]"
              	)
              );
              // php 5 only
              echo json_encode($modx->getPlaceholder("ditto_resource"));
              
                Snippets: GoogleMap | FileDetails | Related Plugin: SSL
                • 16545
                • 358 Posts
                OK, I’ll try this trick as soon as possible....

                One more thing: U’ve just show a simple example of 2 depended documents chain.
                Question: is it possible to use your solution for the multiply depended items?
                Example: dropdown select list for Country -> City -> Category -> Subcategory -> ...
                  • 29774
                  • 386 Posts
                  Sure. In the js you’d probably want to assign and an update function to each of the select elements, as I’ve done for #country. You could update a hidden form value ’mode’ on select change with your selected value so that the php script always checks this hidden value instead:

                  //js
                  $('#country').change(function() { 
                     $('#parents') = $(this).value; //update hidden field with selected value
                     //submit form with ajax and assign callback function
                     ...
                  });
                  
                  //php
                  $parents = isset($_POST['parents'])? intval($_POST['parents']) : 0; // grab from hidden form field
                  $modx->runSnippet('Ditto', 
                  	array(			
                  		"parents"    =>$country,
                  		"depth"      =>1,
                  		"display"    =>"all",
                  		"save"       =>"2",
                  		"tpl"        =>"@CODE [+id+][+pagetitle+]"
                  	)
                  );
                  // php 5 only
                  echo json_encode($modx->getPlaceholder("ditto_resource"));
                  


                  I’ve added in the [+id+] to the Ditto placeholder, since you’ll need it in your updateForm() functions to populate the select menus.
                    Snippets: GoogleMap | FileDetails | Related Plugin: SSL
                    • 32645
                    • 377 Posts
                    I’m stuck on how to use JQuery for select lists, specifically on this problem.

                    1. A user selects a countrry.
                    2. The DB is queried to get a resort linked to a country.

                    I need to query the DB, but I can’t use:

                    url: [~75~]

                    This doesn’t work.

                    In this thread several people have mentioned putting a direct link to a snippet.

                    url: /assets/snippet/someSnippet.php

                    But what *if* you need to connect to MODX, or do a DB call, you need the MySQL parameters, etc and it seems an awful waste to be hard-coding inside the "someSnippet.php" all the libraries you want to use -- when you could just connect to the MODX object.

                    So My quesiton is:

                    1. How do you make somesnippet.php connect to MODX?
                    2. Why is not possible to simply say "OK, I want the ajax to call a php file, but I want to connect to a db, parse a few chunks, and return the result"?

                    Thanks.

                      • 32645
                      • 377 Posts
                      I’m really stuck on this.

                      1. Created blank page with the following html:

                      <!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>Some MODx page</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').change(function() {
                      					$('#loading').show();
                      					var options={
                      						url:		'combo2.php',
                      						dataType:	'json',
                      						timeout:	1000,
                      						error:
                      							function() { 
                      								alert("Error loading document");
                      							}, 
                      						success: 
                      							function() {
                      							}, 
                      							//updateForm //post-submit callback
                      					};
                      
                      					// submit the form 
                      					$('#formname').ajaxSubmit(options);
                      				});
                      		
                      
                      				$('#loading').ajaxStart(function() {
                      					$(this).show();
                      				}).ajaxStop(function() {
                      					$(this).fadeOut("slow");
                      				});
                      
                      			});
                      
                      			function updateForm(data) 
                      			{
                      				//do stuff with returned data, like populate a select menu with cities 
                      				$('#loading').fadeOut(500); 
                      				return true;
                      			}
                      		</script>
                          </head>
                      
                      <style type="text/css">
                      #loading
                      {
                      	display:none;
                      	color:#CC3300;
                      	border:2px solid #CC3300;
                      	background:#FFCCCC;
                      	font-weight:bold;
                      	margin-bottom:1em;
                      }
                      </style>
                      <body>    
                      
                      <div id="loading">Loading...</div>
                      
                      <form id="formname" 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>
                      


                      2. Created "combo2.php" which sits on the document root with the following code:

                      <?php
                      //php
                      $parents = isset($_POST['parents'])? intval($_POST['parents']) : 0; // grab from hidden form field
                      $modx->runSnippet('Ditto', 
                      	array(			
                      		"parents"    =>$country,
                      		"depth"      =>1,
                      		"display"    =>"all",
                      		"save"       =>"2",
                      		"tpl"        =>"@CODE [+id+][+pagetitle+]"
                      	)
                      );
                      // php 5 only
                      echo json_encode($modx->getPlaceholder("ditto_resource"));
                      ?>
                      


                      However the MODX object isn’t loading and thus produces an error.

                      I tried putting the code in a snippet and then setting the Jquery URL to the page such as: http://modx.localhost/a-test/ but this doesn’t return anything.

                      Putting the code outside of MODX on the document root means that the code doesn’t work.

                      ====

                      Questions.

                      1. How do I get the php file to work with MODX objects? I need to do a lot of DB processing.

                      Thanks.