We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47666
    • 43 Posts
    I have several php scripts located in a "scripts" folder that sits in my main directory used for processes sing forms. In one of them I'm setting $_SESSION['product']. HOw can I access in a template file.

    I tried…
    <?php echo $_SESSION['product']; ?>
    


    I also tried creating a snippet with
    <?php echo $_SESSION['product']; ?>
    


    and also
    <?php $_SESSION['product']; ?>
    


    and nothing works. I have it working on a static site, but can't get it to work on modX revo.

    Any ideas?
    [ed. note: aaron4osu last edited this post 12 years, 3 months ago.]
      • 3749
      • 24,544 Posts
      Try this in a snippet:

      <?php 
      return $_SESSION['product'];
        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
        MODX sets its own session, with its own storage and its own session_name. If you are setting a session variable in a script outside of MODX, you have to use the same session. The best thing would be to load a MODX object and let MODX open a session. http://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/loading-modx-externally
          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
          • 47666
          • 43 Posts
          Thanks for the responses.

          I tried using the return statement as BobRay suggested and it i couldn't get it to work.

          Sottwell, I read through the link you posted and I'm not sure if my scripts are considered external or not. They are located inside of a scripts folder which is on the main directory along side of the index.php file, assets folder, etc… see screenshot attached of my directory structure. Is that considered external?

          Here is what I'm trying to do specifically… I'm collected a zip code on the home page form. I'm processing the form with a php file inside of the scripts folder and grabbing their city and state based off of the zip. I'm saving the city and state as session variables and then trying to use the on the page they are led to after submitting the form.

          Under those circumstances what is the best way to handle this?

          I'm new to mode and not really a hardcore developer so please be gentile smiley
            • 4172
            • 5,888 Posts
            If you would do all that inside a MODX - snippet, or include your code within a snippet, you could just store your values into the $_SESSION without creating your own session.

            Why not using formit and a formit-hook to process the form?
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 47666
              • 43 Posts
              I looked at formIt, but couldn't get it to work. Seemed like a learning curve that I don't have time to adjust for. I'm comfortable w/ JQuery and php so I used that for now. I plan on learning how to use formIt and some of the other plugins as I continue to learn modX.

              Right now I have a Jquery ajax call to the php script:
              $.ajax({
                  type: 'POST',
                  url: "scripts/processForm1.php",
                  cache: false,
                  datatype: "html",
                  success: function(data){
                      …              
                  }
              });
              


              if I put the contents of processForm1.php into a snippet, how do I reference it in the ajax call? In other words what would be the path to the snippet?

              would it be?

              $.ajax({
                  type: 'POST',
                  url: "[[snippet-name]]",
                  cache: false,
                  datatype: "html",
                  success: function(data){
                      …              
                  }
              });
              


              If I do that how do you save and access the session variables in side modX?


                • 4172
                • 5,888 Posts
                what does your php-script look like?
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 47666
                  • 43 Posts
                  I have several scripts for difference parts of the site here is the one where I'm saving the session variables and the html where I'm trying to use them.

                  <?php
                  session_start();
                  require_once 'database_connection.php';
                  require_once 'clean_up.php';
                  
                  // Determine if the username is available or not,
                  // and returns a JSON { "valid": true } or { "valid": false }
                  $isAvailable = false;
                  
                  // Get the username from request
                  $zip = mr_clean($_POST['zip']);
                  $query = "SELECT * FROM zipcodes WHERE zipcode = ".$zip.";";
                  $result = mysql_query($query);
                  
                  if ($result) {
                      
                      $num_rows = mysql_num_rows($result);
                      if($num_rows == 1){
                  		$location = mysql_fetch_array( $result );
                  	    $city = $location['city'];
                  	    $_SESSION['city']= $city;
                  	    $_SESSION['location']= $location['city'].', '.$location['state'];
                  
                  	    $isAvailable = true;
                      }	
                  }
                  echo json_encode(array(
                      'valid' => $isAvailable,
                  	));
                  ?>
                  


                  html
                  <div class="col-md-12">
                      <h1>Here are some <?php echo $_SESSION['product']; ?> Insurance Quotes in <?php echo $_SESSION['city']; ?> </h1>
                      <h4 id="continue-form">Continue the form below or call (800) 555-5555 to speak to an agent instantly!</h4>
                   </div>
                  
                    • 4172
                    • 5,888 Posts
                    1. Remove
                    session_start();
                    from your php-file.

                    2. Create a MODX - snippet 'includeScript' with:
                    include $modx->getOption(base_path) . 'scripts/' . $include . '.php';
                    


                    3. Create a new Resource with 'blank' Template and put this into its content:

                    [[!includeScript? &include=`processForm1`]]


                    4. Take the id of that new created Resource, lets say it is '99' and put this into your template:

                    $.ajax({
                        type: 'POST',
                        url: "[[~99]]",
                        cache: false,
                        datatype: "html",
                        success: function(data){
                            …             
                        }
                    });
                    


                    5. Create a snippet called 'getSessionVar' with:

                    return $_SESSION[$var];
                    


                    6. Have html like that:

                    <div class="col-md-12">
                        <h1>Here are some [[!getSessionVar? &var=`product`]] Insurance Quotes in [[!getSessionVar? &var=`city`]] </h1>
                        <h4 id="continue-form">Continue the form below or call (800) 555-5555 to speak to an agent instantly!</h4>
                     </div>



                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 47666
                      • 43 Posts
                      WOW… THANKS for spending the time on this with me Bruno. I'll try this out now!