We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41730
    • 6 Posts
    Hi,

    I have a general knowledge of html, php, and coding logic concepts themselves. I've used other CMS platforms and Modx Revo really shines.

    Having generalized concepts, I'm missing a good chunk of connective understanding, mainly, how to write a php script where the variables are not determined within the script, but are modified by user input.

    Seems like a fairly obvious question, however several hours of internet searches (passing in to the next day) leaves me dead.


    The best information I can find that touches on this plainly, and concisely is:

    http://www.webmonkey.com/2010/02/php_tutorial_for_beginners/

    Here's the quote, because you more than likely won't need to read the link:
    "HTML Forms and PHP

    In our examples so far, we have set variables and then used them all in the same code. This doesn’t make much sense because in those instances, we could have just hard-coded the values instead of using variables."

    Yup.

    Anyway, I've gathered the idea is to line the html up to submit the information to a php script (or file) and then tailor this file to properly read the information that has been submitted to it, but I'm stumped on Modx Revo's version of this.

    The best I can seem to manage is that with syntax such as ||!theSnippet? &variable='whatever is put in here'|| the browser will read that syntax, process the variable through the 'theSnippet' script, and in place of that ||the syntax|| box on the page, the browser will display:

    whatever is put in here (or whatever the php script does to the variable defined in the syntax)



    ?? How can I have the user determine that information? My intuitive guess.. Use form html syntax in a Chunk? call the chunk and then have the Snippet get the input somehow?? whaaaaa?





    What is the most simplest and direct link between an input box (written with html?) and passing that input into a snippet?



    If you could help me, thanks.



    (edit: I'm aware that the input data would probably need to be pushed to a 'separate holding spot' then the php code itself, I'm just looking for the clean standard and I'm clueless as to what that 'spot' is supposed to be. by the link above, I'm writing the html to push the data to a php file and then writing the script to read the info put in the file. Modx has me stumped.) [ed. note: lazerblue last edited this post 11 years, 6 months ago.]
      • 22427
      • 793 Posts
      The front end user fills in the fields in a form; by clicking the submit button the data (preferably after some sanity checking) will be sent to the server and stored in the PHP session array $_SESSION.
      E.g., if there is a input field "foo", submitting the form will set the value $_SESSION['foo'] to the value the user filled in.

      Create a snippet "get_foo":
      <?php
      $output = '';
      if ( isset($_SESSION['foo']) ) {
          $output =  $_SESSION['foo'];
      };
      return $output;
      

      Now where ever you want to get this value on your site, in the template or in the content area of a resource, ..., you just need to call the snippet:
      [[!get_foo]]
      An example:
      <img src="assets/images/[[!get_foo]]" alt="" />

      This could also be done within a chunk, or as the value of a property of a chunk or another snippet, e.g.
      [[$myChunk? &prop=`[[!get_foo]]`]]
      and in many other ways.
      If you want an immediate reaction to the submitted data you may need a reload of the page or a part of it (using Ajax). Some fiddling with caching might be necessary.
        • 41730
        • 6 Posts
        Quote from: ottogal at Oct 19, 2012, 02:14 AM
        The front end user fills in the fields in a form; by clicking the submit button the data (preferably after some sanity checking) will be sent to the server and stored in the PHP session array $_SESSION.

        If you want an immediate reaction to the submitted data you may need a reload of the page or a part of it (using Ajax). Some fiddling with caching might be necessary.

        So I'll check what $_SESSION is about, all of that information is exactly what I needed to know. Appreciate it.




          • 41730
          • 6 Posts
          I might not have clearly indicated the issue I'm having.


          Conceptual:

          Conventionally, it seems that the form tag has an action of a php file, the method is either GET or POST and then after the user selects the properties of the form, the properties of the form are submitted to the php file using whatever method specified.

          This had prompted me to try and understand the standard for Modx on where I was supposed to send the form data without a pre-existing php file or even indicating one at any point.

          You've shown that the form data can be sent to the $_SESSION array and then some example snippets of how to access the data in the array tailored by the submitted form (also possibly the form itself creating the variables and submitting them to the array). Through reading, I now understand how I could set up the form data so that it's in the $_SESSION array to be read properly by a php snippet, but I'm still clueless on how to send the data to the array itself... possibly because I'm trying to use a form tag, or at best, an input tag.

          The majority of conversation and documents regarding these tags deal with sending the info to a php file.



          Realistic:

          I've got a form tag:

          <form  
          
          method="*post*/get" 
          ((unclear on difference, guessing post is correct 
          because of redundancy))  
          
          action="file.php"  
          ((usual?  indicates a file, I assume the 
          form will post to this file))
          
          >
          
            <input type="radio" 
          
          name="cheese"
          ((field, only one value allowed))
          
          value="asiago" 
          ((the value of that field))
          
          >
          You like asiago cheese
          
          </br>
          
            <input type="submit" value="I'm just a name of a button">
          
          </form>



          So once submit is used and radio is checked, this form creates a field cheese with a value of asiago entered, and posts it to the file: file.php

          I'm assuming.


          Provided my assumptions are right, that would mean I need to throw that form at the $_SESSION array once submit is used. I'm just unclear as to the link between a form tag and "submitting the form to a session array" and just don't know what direction to point. =P

          If you take out the action, is modx just going to grab the data submitted postily and do it up right with a home and coziness and everything all up in the $_SESSION array all nice and neat and ripe for the plucking? Is there a magical connection between the finger of god and adam that is infinite and unseeable oh noo


          I'm just assuming there's a tag or two I'm missing that will tell the form to head to the $_SESSION array.


          http://stackoverflow.com/questions/3791414/storing-form-data-as-a-session-variable This provides some clues indicating the logic behind the syntax:

          <form action="" method="post">
          <input type="text" name="picturenum"/>
          <input type="submit" name="Submit" value="Submit!" />
          </form>
          
          <? 
          if (isset($_POST['Submit'])) { 
          $_session['picturenum'] = $_POST['picturenum'];
          } 
          ?> 


          However this seems to be converting post data over to a variable in the session array.



          [ed. note: lazerblue last edited this post 11 years, 6 months ago.]
          • MODx provides a number of add-on packages for doing this, depending on the purpose of the form.The general-purpose form processing add-on for Revo is the FormIt package. Form submission processing is handled using "hooks". FormIt comes with some hooks, such as emailing the form field values to a specified email address.

            http://rtfm.modx.com/display/ADDON/FormIt
              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
              • 41730
              • 6 Posts
              When a form is submitted, it makes a new request to the server, requesting the page specified in the form's action attribute, passing the form field values as either GET or POST values. If no action attribute is provided, by default the browser will request the same page that the form is on. Something on the server has to collect those GET or POST values and process them.



              MODx provides a number of add-on packages for doing this, depending on the purpose of the form.The general-purpose form processing add-on for Revo is the FormIt package. Form submission processing is handled using "hooks". FormIt comes with some hooks, such as emailing the form field values to a specified email address.

              http://rtfm.modx.com/display/ADDON/FormIt


              It is great to know the intention of a hook and that that is the function those are meant to handle.

              It seems FormIt comes with some pre-built hooks. Would you suggest downloading FormIt and "reverse-engineering" one of the pre-built hooks for the function I'm looking for? Seems like a great learning experience, since I'm asking how to hook the form values to be submitted to the $_SESSION array and how those work, are intended to work, and used properly. [ed. note: lazerblue last edited this post 11 years, 6 months ago.]
              • I am sorry if I misunderstood your understanding of how forms work in general. I'm so used to working with eForm for Evo or FormIt for Revo - I haven't worked with a "raw" form for at least five years!

                That is indeed what you should do. You could, of course, write your own form-processing snippet, but FormIt handles a number of other common form processing functions, such as validation and sanitizing form fields.

                There are a few examples of post-hooks (functions to perform after the form has been submitted) in the documentation.

                http://rtfm.modx.com/display/ADDON/FormIt.Examples.Custom+Hook


                You could also install the FormSave package, which is a "hook" form FormIt for saving form submission data; while it does not do quite what you have in mind, it would give a good example of custom processing of the form submission. http://rtfm.modx.com/display/ADDON/FormSave
                  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
                  • 41730
                  • 6 Posts
                  Thank you for the helpful advice and recommendations. It points at a defined array and how it's handled.

                  http://stackoverflow.com/questions/7990161/what-is-the-purpose-of-session-data

                  My attention was split between understanding the concepts, the mechanism and what was the simplest and truest way to pass user input over to scripts for handling between a chunk and a snippet.

                  It's within the idea of a session.

                  https://www.owasp.org/index.php/Session_Management_Cheat_Sheet

                  intensiv in der emesn oy vey
                  [ed. note: lazerblue last edited this post 11 years, 6 months ago.]