tags:

views:

106

answers:

6

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>
+1  A: 

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...
}
Philippe Gerber
+3  A: 
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 array_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

karim79
why is `array_key_exists` better than `isset`. I see only one difference and it seems irrelevant for `$_GET` array.
SilentGhost
@SilentGhost - because GET is an array, it's clearer to use a function that explicitly tests it for a particular key - though arguably both do the same thing for this purpose.
karim79
@SilentGhost - see my edit please.
karim79
Tell my how you're gonna feed $_GET with a null value from the outside? Writing into $_GET inside your script is not sooo good practice.
Philippe Gerber
test.php?test -> var_dump($_GET['test']); -> string '' (length=0)
Philippe Gerber
@Philippe Gerber - Thank you, I've edited the answer. I suspected as much, but don't have access to my test server from here. Anyway, the difference between what they do is useful to know.
karim79
+1  A: 

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'])){
}
Scharrels
+1  A: 

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
}
Alix Axel
isset() still works for this use, although its extremely useful to know all the array functions.
Shadow
+1  A: 

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.

Shadow
+1  A: 

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
Ngu Soon Hui