Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nicholas Westby 2054 posts 7104 karma points c-trib
    Mar 30, 2019 @ 16:25
    Nicholas Westby
    0

    Redirect to Page if Radio Button Selection Has Specific Value

    (Posting as a new thread on behalf of another user to avoid cluttering up another thread: https://our.umbraco.com/packages/backoffice-extensions/formulate/formulate-questions/96504-change-email-recipients-based-on-the-dropdown-selection-plain-javascript-template#comment-305204)

    Is there a way to redirect user to a different page based on radio button selection? I am wondering if this is a built in functionality or will i have to do some JS magic?

    For example: User is presented with two options, yes and no. If they choose "yes" then presses Submit, form works as designed. If a user chooses "no" then presses Submit, form instead of working as designed, simply redirects user to a different page without executing form.

  • Nicholas Westby 2054 posts 7104 karma points c-trib
    Mar 30, 2019 @ 16:28
    Nicholas Westby
    0

    Quick note until I can write a proper response: https://github.com/rhythmagency/formulate/blob/master/src/formulate.frontend/responsive.plain-javascript/steps/render-forms.js#L76-L80

    Formulate has a "formulate: validation: success" that allows for a cancelSubmit property to be set and that seems to contain the form field data.

    Can probably do something with that.

  • Nicholas Westby 2054 posts 7104 karma points c-trib
    Mar 30, 2019 @ 19:20
    Nicholas Westby
    0

    Here's a bit of sample JavaScript code that should send you in the right direction:

    document.querySelector("body").addEventListener("formulate: validation: success", function(e) {
        var form = e.target, data = e.detail, fields = data.fields;
        data.cancelSubmit = true;
        console.log({form, data, fields, e});
        var firstField = fields[0], firstFieldData = {};
        firstField.setData(firstFieldData, {rawDataByAlias: true});
        console.log({firstFieldData});
    });
    

    You should change that quite a bit. Here are some pointers:

    • This is assuming you are using the plain JavaScript template for Formulate (rather than the AngularJs one).
    • Don't do document.querySelect("body") to get the form element. Do something more intelligent that will get an element that wraps the form to ensure you aren't responding to events from multiple forms on the page.
    • Add in your conditions (e.g., cancel the submit based on the field value rather than all the time).
    • Add in your redirect (e.g., when a radio button has a particular value): https://www.w3schools.com/howto/howtojsredirect_webpage.asp
    • Make sure your fields have an alias (otherwise setData won't work how you expect it).
    • Remove the console.log statements (they're just there to help you debug).

    What this script is doing right now is grabbing the value out of the first field and has a couple console.log statements to help guide you. I'm guessing you can figure it out from here.

    Here are some links that may help:

  • Bharti 8 posts 74 karma points
    Apr 01, 2019 @ 09:29
    Bharti
    0

    Hi Nicholas,

    You can use something like below:-

    If($('.class of radiobutton').prop("checked")==true && $('.class of radiobutton').val()=="No") { var href = '@Url.Action("Index", "Controller")?Maps=' + Maps; window.location.href = href; }

  • denis ushakov 11 posts 144 karma points
    Apr 01, 2019 @ 15:19
    denis ushakov
    100

    I got it working by doing the following inside JS file

    var radioButton = document.getElementById("id-of-radiobutton").checked; 
    
                if (radioButton == true) {
                    window.location = "/url/";
                }
                else {
                    window.location = "/aother-url/";
                }
    

    I did this inside wrapper.addEventListener("formulate form: submit: success", function (e) { *... code ...* }); since I needed this redirect on form success.

    Note: This is for Plain JS template.

  • Nicholas Westby 2054 posts 7104 karma points c-trib
    Apr 01, 2019 @ 18:05
    Nicholas Westby
    0

    Keep in mind that the ID's on the elements are dynamic (e.g., if you toss another form on the page, change the field order, or change any fields, that ID may be different).

    Glad to hear you found a solution that works for your situation though.

  • denis ushakov 11 posts 144 karma points
    Apr 01, 2019 @ 18:15
    denis ushakov
    1

    That is actually a very good info! Something that I actually encountered and was able to overcome by changing classes for form wrapper, creating a new JS files as needed that reference my wrapper and then targeting my elements based on that new form class as well.

    Thank you for your help. Formulate truly rocks!

  • Nicholas Westby 2054 posts 7104 karma points c-trib
    Apr 02, 2019 @ 02:53
    Nicholas Westby
    0

    Thank you for your help. Formulate truly rocks!

    Thanks for saying so, Denis! I hope you don't mind, but I've added that as a testimonial here: http://www.formulate.rocks/testimonials

    If you would like me to remove that or to modify the attribution in any way, don't hesitate to let me know. I can be reached here or on my contact page: http://www.nicholaswestby.com/contact

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies