最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Button not refreshing page

programmeradmin1浏览0评论

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'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>
Share Improve this question asked May 21, 2017 at 21:37 Linz DarlingtonLinz Darlington 1111 silver badge2 bronze badges 4
  • 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 Commented May 21, 2017 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 Commented May 21, 2017 at 22:54
  • Related: stackoverflow/questions/33483960/… – Jesse Nickles Commented Jul 1, 2022 at 20:40
  • Also related: wordpress.stackexchange/questions/229139/… – Jesse Nickles Commented Jul 3, 2022 at 8:23
Add a comment  | 

1 Answer 1

Reset to default 1

Because onsubmit event not working with "input" tag. You can see more details in https://www.w3schools/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>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论