Reputation: 69
I wanted to know everything about the "?" of action value of a form. As you see in the code below, We have a form (placed in
a php template) that sends user input to be put in the database with the value of "?" for the action attribute:
<form action="?" method="post">
<div>
<label for="joketext">Type your joke here:</label>
<textarea id="joketext" name="joketext" rows="3" cols="40">
</textarea>
</div>
<div><input type="submit" value="Add"></div>
</form>
We have also another template that shows the data stored in our database and as you see in the code below, this template has
a section with a link so that by clicking that link users can add data (jokes) in our database:
<body>
<p><a href="?addjoke">Add your own joke</a></p>
<p>Here are all the jokes in the database:</p>
<?php foreach ($jokes as $joke): ?>
<blockquote>
<p><?php echo htmlspecialchars($joke, ENT_QUOTES, 'UTF-8');
?>
</p>
</blockquote>
<?php endforeach; ?>
</body>
My first question is why we set the value of href attribute to be "?addjoke"? I mean what is the job of "?" in "?addjoke"?
Well, Another question is: We have an if statement in our index.php file that is like this:
if (isset($_GET['addjoke']))
{
include 'form.html.php';
exit();
}
I can't get the idea of "addjoke" variable in our $_GET array. I mean I know exactly what the $_GET array does but I can't
understand the whole idea of using "addjoke" variable. I mean what is exactly "addjoke" doing here?
So obviously I have a lot of problems with understanding the "?" character in URLs and I need your help :) All I know about
"?" is that this value is used when we need to point to the same page we're on. right?
I have other questions too, but that questions are kept to be asked another time ;) Thank you so much in advance.
Upvotes: 1
Views: 414
Reputation: 24334
If you have a URL like this:
http://www.domain.com/index?myParam1=value1&myParam2=value2
This is a GET request to a server at www.domain.com and it is passing two parameters through.
myParam1 = value1
myParam2 = value2
Everything after the ? is the query string and is treated as query parameters to pass to the server.
So in PHP the value of those parameters can be retrieved using $_GET['myParam1']
The ? itself indicates the beginning of the query string (which contains these parameters and the & separates each parameter.
The form action is the URL that the form will post to. If you specify ? what you are actually saying is the current url + "?". This means it will submit the form to the current url with a ? on the end (Actually this is not really needed)
Note also that query string parameters are only passed in a GET request. When you submit a form with method=POST
the form fields and values are submitted in the request body.
So in your code,
if (isset($_GET['addjoke']))
{
include 'form.html.php';
exit();
}
The two lines are only executed if a query string parameter named addjoke is passed through (e.g. ?addJoke=joke). This means the form is only included when the addjoke parameter is in the URL.
Upvotes: 3
Reputation: 796
The ?-sign in a URL is used to separate parameters from an resource in an HTTP GET-Request. Take the following example:
http://www.example.com/resource/index.php?testvalue=123
In this example the script index.php in directory resource is called and will have set the request parameter named testvalue with value 123. Only the first parameter uses the question mark (?), others will be added with an ampersand (&).
In your example you set an GET parameter without any value. You should also not mix POST and GET parameters, I would recommend to stick to one of them depending on your action (GET for data-retrievel e.g. search, POST for user-input).
If you need any further information I suggest lookup HTTP verbs in wikipedia and PHPs handling of GET and POST parameters.
Upvotes: 1
Reputation: 401
Just to add to what @cowls said, specifically what's going on with ?
vs ?addjoke
. When clicking the link on a page, (let's say example.com/index.php), you would wind up on example.com/index.php?addjoke.
Now, in PHP, addjoke is part of the query string which means it is "set" in $_GET
. This is why if (isset($_GET['addjoke']))
is there; the addjoke form is only included if the went to the ?addjoke
link.
Now, since the form action on is "?"
, once the user submits the a joke they go right back to the normal page, example.com/index.php?
, and the specific addjoke code goes away.
Hope that answers some questions.
Upvotes: 1