We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    You're free to put your scripts anywhere you want. The assets/ folder is a good choice because MODX will never mess with it during upgrades and it makes it easier to back up your own code, but it's up to you.

    Yes, session variables work just as you would expect as long as they are in snippets the are executing under MODX.

    One quick way to find out the value of a variable at some point in the code is to write to the error log:

    $modx->log(modX::LOG_LEVEL_ERROR, 'INCLUDE VALUE: ' . $include;


    or in your case, this might be better:

    $path = $modx->getOption(base_path) . 'scripts/' . $include . '.php';
    $modx->log(modX::LOG_LEVEL_ERROR, 'PATH: ' . $path;
    include $path;


    For more extensive debugging, you can create a function that writes values to a chunk. I write to a chunk called 'debug'. The optional second argument clears the chunk content before writing (make sure the chunk exists first).

    if (!function_exists('myDebug')) {
       function myDebug($msg, $clear = false) {
          $chunk = $modx->getObject('modChunk', array('name' => 'debug'));
          $content = $clear? '' : $chunk->getContent();
          $content .= $msg;
          $chunk->setContent($msg);
          $chunk->save();
       }
    }


    myDebug($path);




      Did I help you? Buy me a beer
      Get my Book: MODX:The Official Guide
      MODX info for everyone: http://bobsguides.com/modx.html
      My MODX Extras
      Bob's Guides is now hosted at A2 MODX Hosting
      • 47666
      • 43 Posts
      I'm still having trouble getting this to work.

      Here is what I've got now:

      inside my home page template I've got this ajax call:
      $.ajax({
            type: "POST",
            url: "[[++site_url]][[~17]]",
            data: $('.home1').serialize(),
            success: function(msg){
                  //alert(msg);        
                  document.location.href = '[[~10]]'; 
            },
            error: function(){
                  alert("error");
             }
      });//close ajax
      


      in include process form 1 (id17) I put a link to the snippet with php:
      [[11]]
      


      then in snippet 11 I've got:
      <?php
      require_once 'scripts/database_connection.php';
      require_once 'scripts/clean_up.php';
      
      // get ip address of user
      if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
          $ip = $_SERVER['HTTP_CLIENT_IP'];
      } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
          $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
      } else {
          $ip = $_SERVER['REMOTE_ADDR'];
      }
      
      if(isset($_POST['zip']) && isset($_POST['product'])){
      	$zip = mr_clean($_POST['zip']);
      	$product = mr_clean($_POST['product']);
      	$_SESSION['product'] = $product;
      
      	$date = date('Y-m-d H:i:s');
      
          $query = "INSERT INTO bsi_lead ( ip, zip, product, submissionDate)
              VALUES( '$ip', '$zip', '$product','".$date."' );";
      
      	$result = mysql_query($query)
      		or die(mysql_error());
      	// get id fom insert
      	$_SESSION['id'] = mysql_insert_id();
      
          $query = "SELECT * FROM zipcodes WHERE zipcode = ".$zip.";";
          $result = mysql_query($query);
      
          $num_rows = mysql_num_rows($result);
          if($num_rows == 1){
      		$location = mysql_fetch_array( $result );
      	    $city = $location['city'];
      	    $city = ucwords(strtolower($city));
      	    $_SESSION['city']= $city;
      	    
          }
      		
      	mysql_close($con);
       
      }
      else{
          echo "no post";
      }
      


      Also, I want to set up the debugging as BobRay suggested but I'm not sure where those files each go and then how to access the errors. I'm so used to just using echo's in php, and alerts in query to troubleshoot.
        • 3749
        • 24,544 Posts
        You don't need the site_url tag here. It will be prepended when MODX processes the link tag.

        url: "[[++site_url]][[~17]]",
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Wouldn't that depend on the link_tag_scheme system setting?
            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
            • 3749
            • 24,544 Posts
            You're right (as usual). The default is -1, which is a relative URL. Otherwise the site_url or the base_url are prepended for all other values of the setting.
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 47666
              • 43 Posts
              Sorry I don't really understand what that means. I changed it to

              url: "[[~17]]",

              but that didn't work either.

              My test is that I'm setting a session variable when it goes to php. then I'm echoing it out. Finally I have an alert set when it comes back to the JQuery Ajax Success block.

              The alert shows that the session variable is coming back empty and not being set.

              Did everything above look ok? especially the include process form 1 (id17) code?
                • 3749
                • 24,544 Posts
                My mistake, you had that right to begin with if you haven't changed the link_tag_scheme setting.

                I don't see your snippet returning anything or producing any output unless the $_POST is empty.
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 47666
                  • 43 Posts
                  Sorry I didn't update the script show in the post. The current version with the alert is:
                  <?php
                  require_once 'scripts/database_connection.php';
                  require_once 'scripts/clean_up.php';
                  
                  // get ip address of user
                  if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
                      $ip = $_SERVER['HTTP_CLIENT_IP'];
                  } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                      $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
                  } else {
                      $ip = $_SERVER['REMOTE_ADDR'];
                  }
                  
                  
                  
                  if(isset($_POST['zip']) && isset($_POST['product'])){
                  	$zip = mr_clean($_POST['zip']);
                  	$product = mr_clean($_POST['product']);
                  	$_SESSION['product'] = $product;
                  
                  	$date = date('Y-m-d H:i:s');
                  
                      $query = "INSERT INTO bsi_lead ( ip, zip, product, submissionDate)
                          VALUES( '$ip', '$zip', '$product','".$date."' );";
                  
                  	$result = mysql_query($query)
                  		or die(mysql_error());
                  	// get id fom insert
                  	$_SESSION['id'] = mysql_insert_id();
                  
                      echo "sessionid: ".$_SESSION['id'];
                  
                      $query = "SELECT * FROM zipcodes WHERE zipcode = ".$zip.";";
                      $result = mysql_query($query);
                  
                      $num_rows = mysql_num_rows($result);
                      if($num_rows == 1){
                  		$location = mysql_fetch_array( $result );
                  	    $city = $location['city'];
                  	    $city = ucwords(strtolower($city));
                  	    $_SESSION['city']= $city;
                  	    
                      }
                  		
                  	mysql_close($con);
                   
                  }
                  else{
                      echo "no post";
                  }
                  
                    • 47666
                    • 43 Posts
                    So in the resource the only thing I need to put is the id of the snippet inside double brackets?

                    [[11]]
                      • 3749
                      • 24,544 Posts
                      I'm not sure if it's the issue, but you should always use return rather than echo in MODX snippets.

                      In fact, if you're going to do this with Ajax, I'd use the MODX infrastructure (especially the modProcessor class) for it.

                      Personally, I would try to get this working without any JS at all to begin with, though you may be more comfortable with JS than I am.
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting