We always retrieve the value in a $_GET['var']
, but is it possible to assign a value as well? I have a php page where at one point, through ajax, I want to stay on the same page but change the query string or do an assignment like this:
$_GET['myvar'] = 'newvalue';
Is this possible?
We always retrieve the value in a $_GET['var']
, but is it possible to assign a value as well? I have a php page where at one point, through ajax, I want to stay on the same page but change the query string or do an assignment like this:
$_GET['myvar'] = 'newvalue';
Is this possible?
Share Improve this question asked Apr 29, 2013 at 13:52 user961627user961627 12.8k43 gold badges148 silver badges212 bronze badges 5- The $_GET variable is just equal to your URL query params . You can change your query string and your work will be done. There is no point to override $_GET – Svetoslav Commented Apr 29, 2013 at 13:56
- How can the query string be changed while on the same page? Is it possible through javascript? – user961627 Commented Apr 29, 2013 at 14:03
- 1 1st and best way is to redirect to grab current query and to refresh page (redirect) to next page .. Other way is with changing browser history(but this works on newer browsers only) - html5demos./history/first – Svetoslav Commented Apr 29, 2013 at 14:08
- Thanks but I need to change the querystring without redirecting the page. Is this possible? – user961627 Commented Apr 29, 2013 at 14:10
- html5demos./history/first - go here there is what you want.. – Svetoslav Commented Apr 29, 2013 at 14:13
3 Answers
Reset to default 2Yes you can override the $_GET. however that is only for that request.
with ajax you do a new request and in the ajax call you can just use diffrent values for the data.
Yes, you can assign to the $_GET array, but it won't change the query string in the URL.
It's probably not the wisest thing to do though, as it will be overwritten in the next request
$_GET
is just like a regular array. The only difference is that the keys of this array will be automatically populated with values that e with the request using HTTP GET. They are called superglobals because they are automatically and globally available anywhere in your script, other wise they behave just like regular arrays.
So if you have a request like mypage.php?key=value
, PHP automatically does something equal to this for you:
$_GET['key'] = 'value';
And just like any regular array, you can overwrite it with a different value. However I really do not see a use case for that unless you are doing some testing or some really weird thingy..