Assistance troubleshooting code to redirect upon form submission

I’m attempting to update a non-profit website to the new v2 forms. The following code is intended to redirect sponsors to a specific page upon submission based on their selections. It runs with no errors, but does not perform the redirect. Similar redirect code is running without issue on the old form version which uses element ID for the input fields rather than the newer field key. What am I missing? Any assistance is appreciated!

// Velo API Reference
import wixLocationFrontend from "wix-location-frontend";

$w.onReady(function () {
    $w("#form6").onSubmit((event) => {
        // Extract field values using the event.fields array
        const fields = event.fields;

        // Define variables
        // Types (dropdown): Monetary, In-Kind, and Monetary & In-Kind
        const selectedTypeField = fields.find(f => f.fieldName === "sponsorship_type");
        const selectedType = selectedTypeField ? selectedTypeField.fieldValue : null;

        // Value (dropdown)
        const selectedValueField = fields.find(f => f.fieldName === "total_value");
        const selectedValue = selectedValueField ? selectedValueField.fieldValue : null;

        // Payment Method (radio button): One Time Online Payment or Cash/Check
        const selectedPayField = fields.find(f => f.fieldName === "payment_method");
        const selectedPay = selectedPayField ? selectedPayField.fieldValue : null;

        // Monthly Payment (radio button): Monthly Online Payment
        const selectedMPayField = fields.find(f => f.fieldName === "monthly_payment");
        const selectedMPay = selectedMPayField ? selectedMPayField.fieldValue : null;

        // Selecting Type "monetary & in-kind" results in 4 and "monetary sponsor" results in 5
        let MyNum = 0;
        if (selectedType === "Monetary Sponsor") { MyNum = MyNum + 1 };
        if (selectedValue === "$750") { MyNum = MyNum + 4 };

        // Redirect for one time online donation
        if (selectedPay === "One Time Payment Online") {
            wixLocationFrontend.to("/sponsorshippayment");
        }
        // Redirect for monthly online donation based on Type field
        else if (selectedMPay === "Monthly Payment Online") {
            switch (MyNum) {
                case 5: { wixLocationFrontend.to("/annual4"); }
                    break;
                case 4: { wixLocationFrontend.to("/annual3"); }
                    break;
                default:
                    break;
            }
        }
    });
});

Nothing immediately obvious is jumping out at me.

2 things I would check:

  • double check that the field keys are accurate and match what is in the forms editor
  • Add console.log’s - they’ll be your best friend in situations like this. I’d recommend logging the variables that you later check in the if statements to confirm that the values you’re expecting and comparing against are the values that are given to you by the forms data.

In v2 forms, fieldValue for dropdowns/radios returns the option’s value, not the label you see in the editor. Those two are often different (e.g. label "$750" but value "750", or label "Monetary Sponsor" but value "monetary_sponsor"). So every === comparison is false, MyNum stays 0, and the switch hits default.

Debug it first - log what you’re actually getting:

$w(“#form6”).onSubmit((event) => {
console.log(JSON.stringify(event.fields, null, 2));
});