We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25551 ☆ A M B ☆
    • 1,231 Posts
    I need some help completing my ajax/eform idea I have been working on the last 24 hours... I may of not done things the most graceful way but it’s working so far.

    Basically I use eform to structure a form and use the verification code, i then wrote a snippet to validate the fields and returns if the user input things correctly or not without refreshing the page. Now I need to redirect the user after they have successfully filled in the fields but everything I have tried seems to fail.

    I tried

    $modx->sendRedirect($modx->makeUrl($modx->config[’site_start’]));

    but this only redirects the area where the ajax div appears when validating the form so it renders the site again within the page. How can I make the redirect work that once the user reaches a certain part of the php that they are redirected to a thank you page.

    I’m using Mootools for the ajax side.

    window.addEvent('domready', function(){
    			$('newJob').addEvent('submit', function(e) {
    				new Event(e).stop();
    				var log = $('log_res').empty().addClass('ajax-loading');
    				this.send({
    					update: log,
    					onComplete: function() {
    						log.removeClass('ajax-loading');
    					}
    				});
    			});
    		}); 


    Everything works great but I would prefer a redirect after the form is completed.
      Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
      AugmentBLU - MODX Partner

      BLUcart - MODX Revolution E-Commerce & Shopping Cart
      • 9130
      • 171 Posts
      Have the javascript onComplete function do the redirect. You might want to test that the operation was successful first by returning a value from the server.
        • 25551 ☆ A M B ☆
        • 1,231 Posts
        Being a novice I am still learning... though I feel like I’m picking things up very fast the last couple weeks.

        I understand to use the onComplete function to do the redirect but I’m unsure how to get a specific value to relate to the ajax to then do the redirect. What I have is the validation checking each input when the user clicks submit. So if the email was not valid it would say so, if the name was input it would say...

        My ajax idea is writing all the inputs to a new database table but only if all the fields validate. It stops the database input if 1 or more fields do not validate.

        At the end of the snippet, after the fields are submitted to the database I have

        echo "

        <strong>Success, your application has been submitted for review</strong>";

        // Return empty string
        return ’’;

        Can I create some sort of function to return TRUE or FALSE and return it to the ajax script so it would either redirect or stay put?

          Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
          AugmentBLU - MODX Partner

          BLUcart - MODX Revolution E-Commerce & Shopping Cart
          • 9130
          • 171 Posts
          You got the right idea, there are several ways to implement it.

          The most obvious way to get success/fail status from the server is using http response header codes. Mootools has build-in onSuccess and onFailure functions to deal with that.

          Other options include sending the response as json and interpreting that on the client side. Another less common option is to send HTML from the server and have script tag with the redirect JS code combined with it.
            • 25551 ☆ A M B ☆
            • 1,231 Posts
            Thanks for the reply... still stuck on this redirect. For now, I made my php register the users IP in the database that if they try to submit the form more than once it will not send due to the IP being logged. It’s what I need but I still would like to get a redirect working in case I change my mind.
              Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
              AugmentBLU - MODX Partner

              BLUcart - MODX Revolution E-Commerce & Shopping Cart
              • 9130
              • 171 Posts
              Can you be a little more informative, what did you try?
                • 25551 ☆ A M B ☆
                • 1,231 Posts
                Thanks for the ideas... I’m still working on it! Trial and error.
                  Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
                  AugmentBLU - MODX Partner

                  BLUcart - MODX Revolution E-Commerce & Shopping Cart