We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18374
    • 69 Posts
    Is it possible to validate/submit an eForm with an AJAX call?

    I have a form that is hidden by default on page load and shown with a JavaScript event. When submitting the form, on page reload the form is once again hidden (even though the eForm validation has successfully fired), meaning the form errors or thank you message is not shown to the user.

    Is there a way I could pass a form field via JavaScript on submission and parse that in eForm to show the form by default on subsequent page loads?

    Thanks again!
      • 18374
      • 69 Posts
      Well I figured out a very nasty way of doing it. tongue

      My checkspam snippet called via &eFormOnValidate was modified to echo a bit of jQuery:

      <?php
      function checkSpam(&$fields, &$vMsg, &$rMsg) {
      	$html =<<<EOF
      		<script type="text/javascript">
      			$("#form-wrapper").show();
      		</script>
      EOF;
      
      	if(!empty($fields['validate'])) {
      		die("spam attempt");
      	} else {
      		echo $html;
      		return true;
      	}
      }
      ?>
      


      Immediately before the form is validated but immediately *after* page refresh, the form is set to unhidden.
        • 19257
        • 141 Posts
        can you post the tpl and eform call you used? I’m kind of lost
          • 18374
          • 69 Posts
          I don’t have the code readily available (it was refactored a while after I posted this), sorry.

          But eForm has several callback parameters you can set to execute snippets or other code at various points during it’s execution. eFormOnValidate is one of them that happened to work for this particular problem. More info on the other events here: http://wiki.modxcms.com/index.php/EForm

          I still think this is a very hacky way to solve the problem; I’m sure some other folks have better solutions.