Here are the solutions to the previous tutorial, ‘Briefing: Using $_GET’. If you missed this tutorial, you can find it here.

(note that there is more than one way to complete each task – I’m just showing you how I would do it)
Task One
The first task asked you to create a simple form that outputted a link. Here’s my solution:
form.php
(I omitted some of the HTML such as the DOCTYPE and <head> to save space)
<html> <body> <form method="post" action="submit.php"> Email: <input type="text" name="email" /><br /> <input type="submit" value="Submit" /> </form> </body> </html>
submit.php
<?php
$email = urlencode( $_POST['email'] );
echo "<a href=\"form.php?email={$email}\">form.php</a>";
?>
The urlencode() is just to make sure that the string that the user entered is encoded to be part of the query string. It’s not completely necessary, but it’s good to use it.
Task Two
The second task asked you to do the same thing for multiple inputs. Here’s my solution:
form.php
<html> <body> <form method="post" action="submit.php"> Username: <input type="text" name="username" /><br /> Password: <input type="text" name="password" /><br /> Email: <input type="text" name="email" /><br /> Boy or Girl: <input type="text" name="gender" /><br /> Interests: <input type="text" name="interests" /><br /> Favorite Music: <input type="text" name="music" /><br /> Favorite Movies: <input type="text" name="movies" /><br /> <input type="submit" value="Submit" /> </form> </body> </html>
submit.php
<?php
$username = urlencode( $_POST['username'] );
$password = urlencode( $_POST['password'] );
$email = urlencode( $_POST['email'] );
$gender = urlencode( $_POST['gender'] );
$interests = urlencode( $_POST['interests'] );
$music = urlencode( $_POST['music'] );
$movies = urlencode( $_POST['movies'] );
echo "<a href=\"form.php?";
echo "username={$username}&";
echo "password={$password}&";
echo "email={$email}&";
echo "gender={$gender}&";
echo "interests={$interests}&";
echo "music={$music}&";
echo "movies={$movies}";
echo "\">form.php</a>";
?>
This task could have been covered with code like above, or with a loop. I’ll give an example of the loop when I show the solution to the third task.
Task Three
The third task is a small addition to the second.
The first file, form.php, will be exactly the same as seen in the second task.
submit.php
<?php
echo "<a href=\"form.php?";
$first = true;
foreach( $_POST as $key => $value ) {
if( !empty( $value ) ) {
$value = urlencode( $value );
if( $first ) {
$first = false;
echo "?{$key}={$value}";
} else {
echo "&{$key}={$value}";
}
}
}
echo "\">form.php</a>";
?>
Basically, you just have to add empty() to your solution for task two where it’s needed.
This concludes the second tutorial. If you have any questions, please leave a comment!