简体   繁体   中英

How to redirect URL when php get string is in current url

Here is my HTML button:

<button class="button-brightgreen" onclick="cancelbutton();">Cancel</button>

Here is my JavaScript redirect:

function cancelbutton() {
    var cancelURL = 'http://localhost:51937/php/searchUsers.php';
    $(location).attr('href', cancelURL);
}

This is on my editUser.php page. This code works fine for me, except when there is a php GET string in the current URL such as ?user_id=19

When there is a GET in the URL and I click the cancel button, it takes me back to the same page (editUser.php) and displays ""

How can I get the redirect to work when there is a GET in the current URL?

You can check if the string user_id is present in the current URL, if present then redirect.

if (window.location.href.indexOf('user_id=') > -1) {
    window.location.href = 'newURL';
}

I figured my problem out. The button was inside a form and when clicked, it would submit the form and the "" was coming from my PHP page. Taking the button out of the form, fixed my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM