1

I need to remove query string value from the url once submit button is clicked. Can i able to do this with jquery?

Current Url:

siteUrl/page.php?key=value

After Page submit:

siteUrl/page.php

Actually i have landing to current page from another one with query string. I need query string value when page loads first time to prefill some details. But once i submitted the form, i need to remove query string value.

I have tried like this.

$('#submit').click(function(){
var newUrl = window.location.href.replace(window.location.search,'');
window.location.href = newUrl;
return false;
});

It makes changes in url as expected. but cant able to get the posted values.

Thanks in advance :)

asked Nov 11, 2016 at 4:55
1
  • 1
    location.search='' Commented Nov 11, 2016 at 4:57

3 Answers 3

1

How about this one. Hope it helps :)

$('#myform').submit(function(event){
event.preventDefault();
var currentURL = window.location.href ;
location.href = currentURL.substring(0, currentURL.indexOf('?'));
});

index.html

<form id = "myform">
<input type = "text">
<input type = "submit" id = "submit" value = "Send">
</form>
answered Nov 11, 2016 at 5:02
Sign up to request clarification or add additional context in comments.

6 Comments

How can i use this? $('#submit').click(function(){ var currentURL = document.location.href; window.location.href = currentURL.substring(0, test.indexOf('?')); }); like above ?
@SyedIbrahim currentURL stores the current URL you are on, then after submitting you can set the current URL using location.href and assign it the value of currentURL.substring(0, test.indexOf('?')); . The should solve your problem :)
@SyedIbrahim Yes, Try that and let me know how it goes.
Sorry bro, it doesn't work :( I still landing on same page with query string.
@SyedIbrahim I just updated my answer and it works just fine :). Please take a look at it.
|
0
function getQueryString(url) {
 return url.split("?")[0];
}
var Url=getQueryString("siteUrl/page.php?key=value");
console.log(Url);

You may try this for getting url

answered Nov 11, 2016 at 5:35

Comments

0

Unfortunately i cant able to do it with javascript or jquery. So i go through with php redirection, now it works.

<?php
if(isset($_POST['Submit'])) {
 // actions
 if(isset($_REQUEST['key']) && ($_REQUEST['key'] != "")) {
 header('Refresh: 1;url='.$_SERVER['PHP_SELF']);
 }
}
?>

Thanks all :)

answered Nov 11, 2016 at 6:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.