17

How do i set the variable that the $_GET function will be able to use, w/o submitting a form with action = GET?

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
dave
  • 14,991
  • 26
  • 76
  • 110
  • 1
    I know this will be rude but i can't help: *why do you want to do that?* – fabrik Mar 24 '11 at 05:44
  • 1
    good question. basically i have links in the top php file, which is included in the index.php, but in order to know which middle page to show, i need the variable that get function will be getting to be set. – dave Mar 24 '11 at 05:49
  • oh, in this case sorry for my misunderstanding i assumed you'll want to tamper $_GET for something else. – fabrik Mar 24 '11 at 05:51

11 Answers11

42

$_GET contains the keys / values that are passed to your script in the URL.

If you have the following URL :

http://www.example.com/test.php?a=10&b=plop

Then $_GET will contain :

array
  'a' => string '10' (length=2)
  'b' => string 'plop' (length=4)


Of course, as $_GET is not read-only, you could also set some values from your PHP code, if needed :

$_GET['my_value'] = 'test';

But this doesn't seem like good practice, as $_GET is supposed to contain data from the URL requested by the client.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 1
    Thx, besides the "not good practice" is there any technical issue in setting `$_GET`? (PHP can be so surprising sometimes...) – Déjà vu Apr 23 '15 at 10:08
  • Best answer. Have no idea why the other was marked as valid. – Flash Thunder Apr 20 '16 at 12:21
  • 2
    You can not set $_GET – Mike Q Oct 12 '17 at 14:45
  • @MikeQ - I just did. Anyone wondering about a use case - site user registration form - on successful registration, change the parameter value from "register a new user" to "sign in" and make sure the code for signing in sits below the code for registering a new user and that you've populated any variables correctly that the login code expects. No need for page redirects. – youcantryreachingme Oct 04 '19 at 03:31
  • Well it's just a variable but the point being I see no good reason to do it. Change the URI instead and redirectr – Mike Q Oct 04 '19 at 16:25
  • Thanks for this - $_GET['my_value'] = 'test'; – Vibhore Jain May 26 '21 at 10:29
11

You can create a link , having get variable in href.

<a href="www.site.com/hello?getVar=value" >...</a>
Gaurav
  • 28,447
  • 8
  • 50
  • 80
  • but lets say how i do it w/o a link? – dave Mar 24 '11 at 05:45
  • probably not. $_GET works with url data or form submission (action =GET). – Gaurav Mar 24 '11 at 05:48
  • after I do this in let's say line 2 and then a javascript runs at say line 92, which asks for variable $_GET['getVar'] will the script get answer as "value". --- just FYI, script is custom google search engine javascript – Rishiraj Purohit Oct 30 '15 at 16:05
3

You can use GET variables in the action parameter of your form element. Example:

<form method="post" action="script.php?foo=bar">
    <input name="quu" ... />
    ...
</form>

This will give you foo as a GET variable and quu as a POST variable.

Sander Marechal
  • 22,978
  • 13
  • 65
  • 96
2

If you want to fake a $_GET (or a $_POST) when including a file, you can use it like you would use any other var, like that:

$_GET['key'] = 'any get value you want';
include('your_other_file.php');

note: i must add that while this is ok for dev/test/debug, it is considered bad programming and i wouldn't recommend using this method in prod. it would be better to pass the processed values to the function/class instead of having it read the $_GET directly.

oriadam
  • 7,747
  • 2
  • 50
  • 48
  • i must add that if it is using for testing/debugging it's ok, but as otherwise it is considered bad programming. it is better to pass the processed values to the function/class instead of having it read a processed $_GET. having said that i know from experience that this is not always possible, for example when including a page that you cannot edit, or an old complicated page that you don't want to mess with... – oriadam Mar 21 '20 at 16:26
  • there are situations where setting _GET like any other variable makes perfect sense and its not bad programming, especially when your code does things on purpose, that it should not otherwise do, like masking an existing site behind an other url structure ;) – Max Lumnar May 11 '23 at 12:03
1

One way to set the $_GET variable is to parse the URL using parse_url() and then parse the $query string using parse_str(), which sets the variables into the $_GET global.

This approach is useful,

  • if you want to test GET parameter handling without making actual queries, e.g. for testing the incoming parameters for existence and input filtering and sanitizing of incoming vars.
  • and when you don't want to construct the array manually each time, but use the normal URL

function setGetRequest($url)
{
    $query = parse_url($url, PHP_URL_QUERY);
    parse_str($query, $_GET);
}

$url = 'http://www.example.com/test.php?a=10&b=plop';

setGetRequest($url);   

var_dump($_GET);

Result: $_GET contains

array (
  'a' => string '10' (length=2)
  'b' => string 'plop' (length=4)
)
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
0

The $_GET variable is populated from the parameters set in the URL. From the URL http://example.com/test.php?foo=bar&baz=buzz you can get $_GET['foo'] and $_GET['baz']. So to set these variables, you only have to make a link to that URL.

Theo
  • 131,503
  • 21
  • 160
  • 205
0

simply write basic code to set get method value in php

Syntax :- $_GET['< get method variable name>']='';

Ex :- $_GET['send']='invoice';

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 18 '22 at 05:45
-1

You could use the following code to redirect your client to a script with the _GET variables attached.

header("Location: examplepage.php?var1=value&var2=value");
die();

This will cause the script to redirect, make sure the die(); is kept in there, or they may not redirect.

Joseph Orlando
  • 183
  • 3
  • 9
-1

For the form, use:

<form name="form1" action="" method="get">

and for getting the value, use the get method as follows:

$value = $_GET['name_to_send_using_get'];
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
sreekanth
  • 1,267
  • 2
  • 13
  • 21
-1

As @Gaurav and @Sander Marechal said, it's possible to add directly at the end of the URL the GET parameters to send to the web page. It works in the majority of cases. But unfortunately, there is an issue with that because an URL cannot accept any character. Some special characters have to be properly encoded to get a valid URL.

Suppose you have an index.php and you want to set a with the value &b= which contains special characters. When I submit the form of the following code, the URL sent is index.php?a=&b= and PHP prints Array ( [a] => [b] => ). So, PHP believes there is two parameters but I only want the parameter a.

You may also notice that the form method is set to POST because if you set it to GET the parameters at the end of the action URL will be ignored. For more details about that see this @Arjan's answer.

<form action="index.php?a=&b=" method="post">
    <input type="submit">
</form>
<?php print_r($_GET); ?>

One solution could be to use the PHP function urlencode. But, as you can see in the following code it's not really convenient to use, especially if you have many parameters to encode. When, I submit this form the URL sent is index.php?a=%26b%3D and PHP prints Array ( [a] => &b= ) as expected.

<form action="index.php?a=<?=urlencode("&b=")?>" method="post">
    <input type="submit">
</form>
<?php print_r($_GET); ?>

But, instead of urlencode function I recommend using hidden input. Thanks to this tag you can send GET parameters dynamically set by PHP. Besides, these parameters are automatically encoded with the URL standard. And you don't need to use & to separate several parameters: use one input to set one parameter. When, I submit this form the URL sent is index.php?a=%26b%3D and PHP prints Array ( [a] => &b= ) as expected.

<form action="" method="get">
    <input type="hidden" name="a" value="<?php echo "&b="; ?>">
    <input type="submit">
</form>
<?php print_r($_GET); ?>
orthose
  • 1
  • 1
  • An HTML form has been already mentioned in several answers. While type of input is irrelevant to the question. Any input will be added to the $_GET variable, no matter of type – Your Common Sense Sep 04 '22 at 10:54
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 08 '22 at 22:02
-2

I know this is an old thread, but I wanted to post my 2 cents...

Using Javascript you can achieve this without using $_POST, and thus avoid reloading the page..

<script>
function ButtonPressed()
{
window.location='index.php?view=next'; //this will set $_GET['view']='next'
}
</script>

<button type='button' onClick='ButtonPressed()'>Click me!</button>

<?PHP
 if(isset($_GET['next']))
    {
          echo "This will display after pressing the 'Click Me' button!";
    }
 ?>
SkriptZor
  • 25
  • 2