We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33453
    • 141 Posts
    I have a form (chunk) which uses basic Javascript onchange validation. When the form is submitted it goes to a page which invokes a snippet. This performs further server-side validation, basically checking that the supplied data exists in the database.

    It’s all working pretty well except that, if the data does not exist, we have already arrived at the destination page. This will display no information or, at best, a stupid error message telling the user that the input on the previous page is incorrect. The user then has to backstep and try again.

    My question is, is there any way I can call an ’onsubmit’ javascript function which will then call the php validation and either display an error message or proceed to the required page, as appropriate, depending on the result?

    Thanks.
    • There are ways to return to the originating page, with the originally submitted data restored to the form, and an appropriate message. Basically, you use the same page for your form and for the destination, just using "if" conditions to determine if the form has been submitted yet, display the form if not, process the input if yes, and display the form (pre-loaded with the user’s input) with error message if yes but with errors. The FlexSearchForm snippet is an excellent example of this.

      Then there is "Ajax", an old (actually Microsoft) Javascript technology with a catchy new name and more tutorials than you can shake a stick at now that it’s become popularized. In this, the Javascript XMLHttpRequest object takes your data, calls the PHP script, using either POST or GET, and handles the data the script returns (in xml form or not, as you choose), all without refreshing the page.

      Here’s a nice new introduction to Ajax:

      http://www-128.ibm.com/developerworks/web/library/wa-ajaxintro1.html?ca=dgr-lnxw01MasterAJAX

      That article has plenty of links to more if it seems like what you have in mind.

      Of course, the problem with this approach is that your user may have Javascript disabled, or may be using assistive technology (such as a screen reader for the blind) that cannot use the Javascript.
        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
        • 33453
        • 141 Posts
        Thanks Susan,

        It did occur to me during a rather lengthy wait at the doctor’s this afternoon that one way to go would be to call the validation and redirect forward or backward depending on the result.

        As for AJAX, I’m not quite ready for that yet. As far as I know MODx does not have an AJAX engine yet (next release?). I too have wondered how such sites will fare when Javascript is not enabled - just won’t work I guess.

        In the fullness of time I am hoping to have a web 2.0, a vanilla HTML, and a text only front end selectable by the user but, for now, I think I will stick to the old-fasioned way.
        • For managing a site, I think it’s a very fair requirement to have JS enabled. From the standpoint of front ends, that depends on who’s surfing your site...

          JS being on is a pretty safe assumption to make at this point I think though.
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me