What does the ?do here?

I found a great book PHP and MySQL: Novice to Ninja by Kevin Yank. It’s just what I want to know about.

Following the instructions, I have built a jokes database. There are 4 files: index.php, jokes.html.php, form.html.php and error.html.php

The file index.php first shows the content of the db in the browser and allows me to add a joke or delete a joke. It works fine!

I don’t really understand what the ? in the ‘add a joke’ link or in the 'form action=?addjoke does.

Can you please explain it?

index.php first logs me into the db then calls jokes.html.php

while ($row = $result->fetch())
{
$jokes[] = array('id' => $row['id'], 'text' => $row['joketext']);
}
include 'jokes.html.php';

jokes.html.php has this:

<body>
<p><a href="?addjoke">Add your own joke</a></p>
<p>Here are all the jokes in the database jokes:</p>

<p>Each joke is in a form, the submit button is the delete button. If you click delete, the joke will be gone.</p>

<?php foreach ($jokes as $joke): ?>
<form action="?deletejoke" method="post">
<blockquote>
<p>
<?php echo htmlspecialchars($joke['text'], ENT_QUOTES,'UTF-8'); 
?>
<input type="hidden" name="id" value="<?php echo $joke['id']; ?>">
<input type="submit" value="Delete">
</p>
</blockquote>
</form>
<?php endforeach; ?>
</body>

Both the ‘a href’ and ‘form action=’ have a ?:

`href="?addjoke"`
action="?deletejoke"

index.php also has for example this to add a joke:

if (isset($_GET['addjoke']))
{
include 'form.html.php';
exit();
}

The question mark starts the query string that holds the $_GET parameters

1 Like

So, without ? it would not work?

It is a way to use relative URL’s. You should read about the difference between a absolute- and a relative URL. Suppose that we can find your PHP page at http://localhost/index.php. Then the form will be POST to the URL 'http://localhost/index.php' + '?addjoke' which makes http://localhost/index.php?addjoke.

Now when the user presses the ‘Add Joke’ button the form data will be submitted to that URL.
Inside PHP you will receive two types of variables.

  1. the $_POST data which is holding a new joke to be stored into the database
  2. a $_GET variabele with the name ‘addjoke’ to indicate which action to perform.

To make everything a little less fancy for you you could dump all the POST and GET variables like this:

echo 'GET data: <pre>' . print_r($_GET, true) . '</pre>';
echo 'POST data: <pre>' . print_r($_POST, true) . '</pre>';
1 Like

@benanamen is correct.

it signals there are URL params starting…

subsequent ones will begin with an & sign:

www.somedomain.com?param1
www.somedomain.com?param1=1&param2=6

etc…etc.

you have JUST a param there (and check if it exists)… or it can hold data/a value

1 Like

Official description from the specs…

The question mark ("?", ASCII 3F hex) is used to delimit the boundary between the URI of a queryable object, and a set of words used to express a query on that object.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service