1

I'm creating a WooCommerce site where the archive page shows products related to the city the user is interested in "London" or "Manchester.

On the /shop page of the website I've created a banner which will either have a button saying 'Switch to London' (if you're in Manchester) or 'Switch to Manchester' (if you're in London).

The content of this banner is set based on an IF function on which location you're in.

Code below.

But, if you press the button it makes the submission BUT it doesn't properly refresh the page, so it doesn't reload the IF statement and you can't click the button and change location again.

If you refresh the page it's all sorted.

I've tried:

Adding some JavaScript to the button: onSubmit="window.location.reload()" which does nothing AND

Adding something to one of the PHP functions which is fired by the button, but this just breaks things.

Any thoughts greatly appreciated and thanks in advance.

        <div class="switch-cities col-sm-12">
        <?php
            $user_id = get_current_user_id();

            if ( wc_memberships_is_user_active_member( $user_id, 'London' ) || wc_memberships_is_user_active_member( $user_id, 'Placeholder' ) ) {
                //$user_id = get_current_user_id();
                $newplan = 148230;
                $oldplan = 148169;

                if(array_key_exists('switchman',$_POST)){
                    lnz_wc_membershipswitch( $oldplan, $newplan, $user_id);
                return;
                }
                ?>
                    <form method="post">
                        <input type="submit" name="switchman" id="switchman" value="Switch to Manchester" onSubmit="window.location.reload()" /><br/>
                    </form>
                <?php;
                } elseif ( wc_memberships_is_user_active_member( $user_id, 'Manchester' ) || wc_memberships_is_user_active_member( $user_id, 'Placeholder' ) ) {
                //$user_id = get_current_user_id();
                $newplan = 148169;
                $oldplan = 148230;

                if(array_key_exists('switchlon',$_POST)){
                    lnz_wc_membershipswitch( $oldplan, $newplan, $user_id);
                return;
                }
                ?>
                    <form method="post">
                        <input type="submit" name="switchlon" id="switchlon" value="Switch to London" onSubmit="window.location.reload()" /><br/>
                    </form>
                <?php
                } else {
                    print 'something is wrong - not in a city';
                }
        ?>
        </div>
4

1 Answer 1

1

Because onsubmit event not working with "input" tag. You can see more details in https://www.w3schools.com/jsref/event_onsubmit.asp

Supported HTML tags: form, keygen

Replace code to

<form method="post"  onSubmit="window.location.reload()">
   <input type="submit" name="switchlon" id="switchlon" value="Switch to London" /><br/>
</form>

or

<form method="post" >
   <input type="submit" name="switchlon" id="switchlon"  onClick="window.location.reload()" value="Switch to London" /><br/>
</form>
2
  • Hi Dat Nguyen. Didn't seem to work but this onsubmit="setTimeout(function () { window.location.reload(); }, 10)" did. No idea why! Thanks for the help though. May 22, 2017 at 17:28
  • @LinzDarlington should link your source: stackoverflow.com/a/42817540/1718491 Jul 1, 2022 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.