Reputation: 141220
How can you transfer a variable such as ask_question
in the URL such that it changes the outlook of your index.php by given rules?
I have the following link in my index.php
<a href="index.php?ask_question">Ask question</a>
which however does not work since I do not know how you can refer to the ask_question -variable in the URL. If I knew that, I could make read it and then customize my index.php according to the name in the URL.
I could also make the following link, but it would cause me to have a lot of duplicate data such as PHP code for login -form.
<a href="ask_question.php">Ask question</a>
My ask_question.php -form
<form method="post" action="handlers/handler_question.php">
<p>Title:
<input name="title" type="text" cols="92" />
</p>
<p>Question:
<div id="wmd-container" class="resizable-textarea">
<textarea id="input" class="textarea" tabindex="101" rows="15" cols="92" name="body" /></textarea>
</div>
</p>
<p>Tags:
<input name="tags" type="text" cols="92" />
</p>
<input type="submit" value="OK" />
</form>
Upvotes: 2
Views: 1763
Reputation: 342655
if(isset($_GET['ask_question')) {
...do something...
}
Better still, since the $_GET superglobal is an array:
if(array_key_exists('ask_question',$_GET)) {
...do something...
}
Also, the manual says this on the use of array_key_exists
vs. isset
:
isset() does not return TRUE for array keys that correspond to a NULL value, while a
rray_key_exists()
does.
However, you cannot pass a null value, it will just show up in PHP as the empty string, so both isset
and array_key_exists
do exactly the same thing in this context.
Read Predefined Variables:
http://www.php.net/variables.predefined
And look up the functions isset
and array_key_exists
Upvotes: 3
Reputation: 83254
Why not do something like
<a href="index.php?query= ask_question"/>
in your html code?
Then in your index.php
script, you can check whether the query
is ask_question
by following commands:
if($_GET['query']=='ask_question')
// change the layout as you wish
else
// proceed with not changing the layout
Upvotes: 1
Reputation: 26061
Use isset($_GET['NAME_IN_URL'])
. This will return true
or false
which you can evaluate with a simple if else statement.
Example:
if (isset($_GET['ask_question']) {
//Echo your form...
}
else {
//Echo the normal index.php
}
Also note that when linking to a query string, you can just do <a href="?ask_question">
, that way it still works if the file gets renamed.
Upvotes: 1
Reputation: 154563
You can access with the $_GET superglobal.
In this case (since you are not defining a value for ask_question), you can do:
if (array_key_exists('ask_question', $_GET) === true)
{
// do stuff
}
Upvotes: 1
Reputation: 3055
Query parameters are saved to the $_GET object. You can test the existence of a parameter using isset:
if(isset($_GET['ask_question'])){
}
If you want te freedom of passing variables as either GET or POST, you can use the $_REQUEST superglobal:
if(isset($_REQUEST['ask_question'])){
}
Upvotes: 1
Reputation: 17836
All GET variables (those in an URL) end up in an array called $_GET. With isset()
you then can check whether a particular variable was passed in the URL.
if (isset($_GET['ask_question'])) {
// go...
}
Upvotes: 1