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

    I am trying to get the value of text input form field into a snippet, please help.

    I am using the click event of a button to call the snippet.

    What I have tried:


    • Use placeholder into the input tag's value attribute
    • have the snippet set the value first with setPlaceholder then use getPlacehoder to retrieve it


    • Place the input tag in a chunk, tried placeholder and TV into the input tag's value attribute
    • have the snippet get the chunk using getObject then tried getContent or get() the placeholder TV

    I have tried a bunch of combinations interchanging placeholders and TVs, but nothing works.

    Thank you in advance,
      • 4172
      • 5,888 Posts
      hmm... not easy to understand, what you are trying.

      Quote from: pd5023 at Feb 06, 2016, 01:05 PM
      Hi,

      I am trying to get the value of text input form field into a snippet, please help.

      I am using the click event of a button to call the snippet.

      you are talking about calling a snippet by the click event of a button.
      how do you do that?
      Is it an ajax-call?
      If so, what is the code so far, you have used?
      And what is the code of your snippet?
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 51760
        • 6 Posts
        Quote from: Bruno17 at Feb 06, 2016, 01:47 PM
        hmm... not easy to understand, what you are trying.

        Quote from: pd5023 at Feb 06, 2016, 01:05 PM
        Hi,

        I am trying to get the value of text input form field into a snippet, please help.

        I am using the click event of a button to call the snippet.

        you are talking about calling a snippet by the click event of a button.
        how do you do that?
        Is it an ajax-call?
        If so, what is the code so far, you have used?
        And what is the code of your snippet?

        Thank you Bruno for the quick response.

        on the resource page I have this basic form for testing purposes:
        <div><form id="usrForm"><table width="100%" border="0"><tr><td id="data" >First Name:</td><td>[[$chname]]</td></tr><tr><td > </td><td><input id="usr_save" type="button" value="Save"/></td></tr></table></fieldset></form></div>


        I am using a chunk called chname that contains the input field like this:
        <input id="fname" name="fname" type="text" value="[[+phname]]">


        in this chunk, I am trying to use a placeholder (phname), but i have also tried with a TV.

        In the resource, I am using:
        	jQuery( "#usr_save" ).on("click", function() {
                var test = '[[!testsnippet]]';
                console.log(test);
            })
        ;

        and my testsnippet is:
        <?php
        $chunkName = 'chname';
        $ch = $modx->getObject('modChunk',array('name' =>$chunkName));
        
        $msg = $ch->getContent(); //just return the html of the chunk, not the value
        //$msg = $ch->getValue('phname'); //crashes the code
        //$msg = $ch->get('phname'); //does not return anything
        //$msg = $ch->get(); //does not return anything
        return $msg;


        The above code is just one example of what i have tried so far.

        Again, I am trying to send to value of a text field from a form to a snippet.
        I am sure I am over complicating this.
          • 4172
          • 5,888 Posts
          The snippet gets parsed serverside, before the page was loaded.
          It doesn't get parsed clientside, when you click the button.
          To run a snippet and get the result back into the page clientside, you will need an ajax-request to a MODX-resource, where the snippet is running.

          Perhaps you can tell a bit more about that, what you are after at all.


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

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 51760
            • 6 Posts
            Hi Bruno,

            What i am trying to do is take opportunity of the great feature that snippets are.

            Yes, a snippet is parsed before page load, but it does runs again when i click the button. So, the way I see it, snippets are some sort of a link/pipe... between resource and the server.
            I am already using snippets on page load to get data from the database, and I love it. I am also using ajax to call a snippet through a page with an empty template and it works fine, I am just thinking that it would be easier and faster if i could connect directly to the snippet with dynamic data through javascript/jQuery events.

            I think I am making progress, but I am still having issues.

            Consider the following code:
            jQuery( "#usr_save" ).on("click", function() {
                var new_val = jQuery("#someinputid").val();
                var test = '[[!testsnippet? &msg=`'+new_val+'`]]';
                console.log(test);
            })

            and my snippet:
            <?php
            return $msg;

            If I just run the code like this, it will return what ever is typed in the text input (good).

            However I cannot manipulate the $msg variable inside the snippet.
            example, let say that I type "this_is_a_new_val" into the text box, if I try to get part of $msg with explode like this:
            <?php
               $t = substr($msg, 0, 30);
               $a = explode("_", $t);
               return $a[1];
            

            it will not return the expected "is" but it will return "val+", meaning that internally the snippet interpret what is inside the ticks.
            Note: I need the substr line or it will generate an error.

            Any idea on how to get to the dynamic string will be greatly appreciated.

            Thank you for your help
              • 3749
              • 24,544 Posts
              If the snippet runs again, it must be because the page is reloading (which JS is usually used to prevent).

              If the page reloading is OK, you might as well skip the JS code and just have the snippet process the $_POST value from a form.

              If you don't want the page to reload, you'll have to have the JS perform an Ajax call to another page containing the snippet (or a processor file) and prevent the submission of the form.
                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