Change single variable value in querystring
Use
parse_url()
to extract the query string from the URLparse_str()
to split the query string into an arrayarray_merge()
to add a new array"b" => 5
http_build_query()
to re-build a query stringThe remaining parts from the first step (protocol, host, path...) to re-build the full URL or - if you have the
HTTP
pecl extension - ahttp_build_url()
withHTTP_URL_JOIN_QUERY
will alleviate much of the work.
How can I add or update a query string parameter?
I wrote the following function which accomplishes what I want to achieve:
function updateQueryStringParameter(uri, key, value) {
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
return uri.replace(re, '$1' + key + "=" + value + '$2');
}
else {
return uri + separator + key + "=" + value;
}
}
Is it possible to change the query string value
does it means that assigning values to the query string only at the time of redirection
Yes. The query string comes from the request the browser makes. You can't change a request that is being processed. You'll have to redirect using the new query string values.
Adding/Modify query string / GET variables in a url with javascript
No need to use jQuery on this one. Regular Expressions and string functions are sufficient. See my commented code below:
function addParameter(url, param, value) {
// Using a positive lookahead (?=\=) to find the
// given parameter, preceded by a ? or &, and followed
// by a = with a value after than (using a non-greedy selector)
// and then followed by a & or the end of the string
var val = new RegExp('(\\?|\\&)' + param + '=.*?(?=(&|$))'),
parts = url.toString().split('#'),
url = parts[0],
hash = parts[1]
qstring = /\?.+$/,
newURL = url;
// Check if the parameter exists
if (val.test(url))
{
// if it does, replace it, using the captured group
// to determine & or ? at the beginning
newURL = url.replace(val, '$1' + param + '=' + value);
}
else if (qstring.test(url))
{
// otherwise, if there is a query string at all
// add the param to the end of it
newURL = url + '&' + param + '=' + value;
}
else
{
// if there's no query string, add one
newURL = url + '?' + param + '=' + value;
}
if (hash)
{
newURL += '#' + hash;
}
return newURL;
}
And here is the Fiddle
Update:
The code now handles the case where there is a hash on the URL.
Edit
Missed a case! The code now checks to see if there is a query string at all.
How can I change correctly the parameters in the query string for a URL after the event handler onclick?
Try this -
<script>
function updateParams(abba){
if(abba.href.search('quantity-0') > -1) {
var a = abba.href.replace('quantity-0', 'add-0');
abba.setAttribute('href', a);
}
else {
var a = abba.href.replace('add-0','quantity-0');
abba.setAttribute('href',a);
}
}
</script>
Hope this helps. Thanks!
Will Query String value change if multiple users access same page?
Based on your particular case above, the query string values between user1 and user2 will be totally independent. That means the first file uploaded by user1 will be saved to /txt
folder and the second file uploaded by user2 will be saved to /img
folder.
However, if both user1 and user2 upload files with the exact same name let's say abc.txt
, then the first file from user1 will be overwritten by user2. You need to check the existence of the uploaded file name in page1 to avoid that. I would suggest renaming the uploaded file name by adding some incrementing number. For example, if user1 just uploaded abc.txt
in page1, then when user2 also uploads abc.txt
a bit later, change the file name to abc-1.txt
, or to abc-2.txt
if abc-1.txt
also exists and so on. Hope that helps.
Search and replace specific query string parameter value in javascript
a_href = a_href.replace(/(test_ref=)[^\&]+/, '$1' + updated_test_ref);
Updating existing URL querystring values with jQuery
Get query string values this way and use $.param to rebuild query string
UPDATE:
This is an example, also check fiddle:
function getQueryVariable(url, variable) { var query = url.substring(1); var vars = query.split('&'); for (var i=0; i<vars.length; i++) { var pair = vars[i].split('='); if (pair[0] == variable) { return pair[1]; } }
return false; }
var url = 'http://www.example.com/hello.png?w=100&h=100&bg=white'; var w = getQueryVariable(url, 'w'); var h = getQueryVariable(url, 'h'); var bg = getQueryVariable(url, 'bg');
// http://www.example.com/hello.png?w=200&h=200&bg=white var params = { 'w':200, 'h':200, 'bg':bg }; var new_url = 'http://www.example.com/hello.png?' + jQuery.param(params);
php; remove single variable value pair from querystring
I'd use http_build_query, which nicely accepts an array of parameters and formats it correctly. You'd be able to unset the edit parameter from $_GET
and push the rest of it into this function.
Note that your code has a missing call to htmlspecialchars()
. A URL can contain characters that are active in HTML. So when outputting it into a link: Escape!
Some example:
unset($_GET['edit']); // delete edit parameter;
$_GET['pagenum'] = 5; // change page number
$qs = http_build_query($_GET);
... output link here.
Related Topics
Using Variables in MySQL Update (Php/Mysql)
PHP SQL Join Together Multiple Tables from Different Databases
Forget Password Page, Creating a Generated Password to Email to the User
Filling Gaps in Dates Returned from Database - Pure SQL Solution Possible
Calendar Using JavaScript/ PHP/ MySQL
Executing a Powershell Script from PHP
.Htaccess - Redirect Subdomain to Folder
PHP Readfile VS. File_Get_Contents
How to Define Global Functions in PHP
Downloading a File with a Different Name to the Stored Name
How to Replace Double Quotes with Single Quotes
How to Display Xml in HTML in PHP