We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49499
    • 48 Posts
    patrickappelman Reply #1, 10 years ago
    I have a simple form with a custom hook that submits 5 times... I have tried everything but cannot seem to figure out why this form is submitting so many times... What's weird is if I follow my custom hook with a redirect hook, the form only submits once. But as long as I am only calling my hook, it submits 5 times.

    form.ampFindPerson [chunk]
    Here is my form.
    [[!FormIt?
    	&hooks=`spam,ampGetUser`
    	&submitVar=`amp_find_person_submit`
    ]]
    
    <form id="amp_find_person_form" action="[[~[[*id]]]]#FindPerson" method="post" class="c-form">
    	<input type="hidden" name="nospam:blank" value="" />
    	<div class="row">
    		<div class="small-12 columns">
    			<input type="email" name="amp_find_person_email_address" id="amp_find_person_email_address" />
    		</div>
    	</div>
    	<div class="row">
    		<div class="small-12 columns">
    			<button id="amp_find_person_submit" name="amp_find_person_submit" type="submit" value="Find Person">Find Person</button>
    		</div>
    	</div>
    </form>
    
    [[!$validate.ampFindPerson:jsToBottom]]


    validate.ampFindPerson [chunk]
    This immediately disables the submit button from being clicked multiple times. However, I do not think this is a client-side issue...
    <script>
    	$("#amp_find_person_form").submit(function(event) {
    		$("#amp_find_person_submit").attr("disabled", true);
    	}); // Disable multiple submits
    </script>


    ampGetUser [snippet]
    All I am doing is writing a log to the error.log to see if the form submitted. In the log file, I see 5 new entries every time I submit the form...
    <?php
    $modx->log(modX::LOG_LEVEL_ERROR,'The form has been submitted.');
    return true;


    error.log
    [2016-09-06 09:20:06] (ERROR @ /Users/patrickappelman/Sites/gr/core/cache/includes/elements/modsnippet/168.include.cache.php : 2) The form has been submitted.
    [2016-09-06 09:20:06] (ERROR @ /Users/patrickappelman/Sites/gr/core/cache/includes/elements/modsnippet/168.include.cache.php : 2) The form has been submitted.
    [2016-09-06 09:20:06] (ERROR @ /Users/patrickappelman/Sites/gr/core/cache/includes/elements/modsnippet/168.include.cache.php : 2) The form has been submitted.
    [2016-09-06 09:20:06] (ERROR @ /Users/patrickappelman/Sites/gr/core/cache/includes/elements/modsnippet/168.include.cache.php : 2) The form has been submitted.
    [2016-09-06 09:20:06] (ERROR @ /Users/patrickappelman/Sites/gr/core/cache/includes/elements/modsnippet/168.include.cache.php : 2) The form has been submitted.


    Has anyone encountered this before? Any suggestions?

    This question has been answered by BobRay. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      If the "action" of your form is set to the current page, FormIt will execute over and over, as will the postHook, as long as the submit variable is set in the $_POST.

      You might try setting the submit variable to '' in your posthook.
        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
        • 49499
        • 48 Posts
        patrickappelman Reply #3, 10 years ago
        Quote from: BobRay at Sep 06, 2016, 09:58 AM
        You might try setting the submit variable to '' in your posthook.

        Hi BobRay, even when setting the value of my submitvar to '' in my posthook, it still writes 5 lines to error.log.

        ampGetUser [snippet]
        <?php
        $_POST['amp_find_person_submit'] = '';
        $submitvar = $hook->getValue('amp_find_person_submit');
        $modx->log(modX::LOG_LEVEL_ERROR,'The form has been submitted. Submit var is ' . $submitvar);
        return true;

        (I wrote the value to the error log to verify that I did indeed change the value of the submit variable.)

        Is there any way I can keep formit from executing over and over again? I want to end up on the current page because I am going to output results below the search form. [ed. note: patrickappelman last edited this post 10 years ago.]
          • 49499
          • 48 Posts
          patrickappelman Reply #4, 10 years ago
          Quote from: patrickappelman at Sep 06, 2016, 10:22 AM
          Hi BobRay, even when setting the value of my submitvar to '' in my posthook, it still writes 5 lines to error.log.

          I was mistaken... the submit variable was not actually changed. I was moving too fast and didn't save. It is working now when removing the value from the post variable.
            • 13428 ☆ A M B ☆
            • 1,031 Posts
            Do you call the FormIt snippet in the content of the resource? Are there multiple [[*content]] tags in the template? Do you use a snippet that fills the description html tag or similar with the content field?

            That could lead to multiple execution of the FormIt call and to multiple execution of the custom hook.
              • 49499
              • 48 Posts
              patrickappelman Reply #6, 10 years ago
              Quote from: Jako at Sep 06, 2016, 12:23 PM
              Do you call the FormIt snippet in the content of the resource? Are there multiple [[*content]] tags in the template? Do you use a snippet that fills the description html tag or similar with the content field?

              That could lead to multiple execution of the FormIt call and to multiple execution of the custom hook.

              Hi Jako. No, the snippet is called only as a post hook and isn't actually on the page anywhere. The form is located in an uncached chunk in the content of the resource. There is only one content tag in the template.