MrCodeWeaver
MrCodeWeaver

Reputation: 435

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?

Upvotes: 0

Views: 250

Answers (2)

MrCodeWeaver
MrCodeWeaver

Reputation: 435

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.

Upvotes: 0

Tushar
Tushar

Reputation: 87213

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';
}

Upvotes: 1

Related Questions