How can redirect the user back to the same page with the existing Query Strings but have 1 added/modified like "page".
I suppose 1 method is:
$_SERVER['QUERY_STRING']
into an arraypage
exists in the array, modify the value, else add ithttp_build_query
to get the query string to append to $_SERVER['PHP_SELF']
but is there a better/more direct way?
set('PARAM_HERE', VALUE_HERE); history. pushState(null, '', url); This will preserve everything about the URL and only change or add the one query param. You can also use replaceState instead of pushState if you don't want it to create a new browser history entry.
Edit / Update a ParameterAfter setting the new value you can get the new query string with the toString() method. This query string can be set as the new value of the search property of the URL object. The final new url can then be retrieved with the toString() method of the URL object.
Two quick functions, one for adding a query string variable and another to remove a query string variable. Either of these functions can be used in any PHP script to modify query-string parameters. Here is a function that will add the specified query-string variable (key and value) to the specified URL:
With the help of the parse_str () function, you can parse query strings into variables. The syntax of this function is like so: Now, let’s see examples of how to use these two functions for getting the parameters from the URL string.
Almost all developers at least once in their practice have wondered how to get parameters from a URL string with the help of PHP functions. In our tutorial, we will provide you with two simple and handy functions such as pase_url () and parse_str () that will allow you to do that. Read on and check out the options below.
If parameters are used, they are referred to in the query string as $1, $2, etc. The same parameter may appear more than once in the query; the same value will be used in that case. params specifies the actual values of the parameters. A null value in this array means the corresponding parameter is SQL NULL .
Just to make sure you know about it, use parse_str
to parse your query string:
<?php
parse_str($_SERVER['QUERY_STRING'], $query_string);
$query_string['page'] = basename($_SERVER['PHP_SELF']);
$rdr_str = http_build_query($query_string);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With