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>
  • I managed to sort the issue by adding this to the end of one of the PHP functions: echo '<META HTTP-EQUIV="Refresh" Content="0; URL=">' But it feels like a slightly inelegant solution! – Linz Darlington May 21 '17 at 21:58
  • You shouldn't use the same URL for both locations, if you used different URLs for Manchester and London the problem would go away, and the pages would now be cachable making it a lot faster – Tom J Nowell May 21 '17 at 22:54
0

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>
  • 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. – Linz Darlington May 22 '17 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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