Solution: Using $_POST

Here are the solutions to the previous tutorial, ‘Using $_POST’. If you haven’t checked out the original tutorial yet, you can find it here.

Solution: Using $_POST

(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 with one input that submitted to a script that would print the value that the user inputted. 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

echo $_POST['email'];

?>

This part isn’t too complex. Just make sure that the action of your form is set to the script. Also, make sure that the name of your input and the $_POST variable that you are using are the same (mine was ‘email’). It’s case sensitive.

Task Two

The second task asked you to do basically the name thing as the first, just expanded. It also gave you a format for outputting the data.

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

echo "username => {$_POST['username']}<br />";
echo "password => {$_POST['password']}<br />";
echo "email => {$_POST['email']}<br />";
echo "gender => {$_POST['gender']}<br />";
echo "interests => {$_POST['interests']}<br />";
echo "music => {$_POST['music']}<br />";
echo 'movies => ' . $_POST['movies'] . '<br />'; // there's more than one way to write it

?>

There’s more than one way to set up the strings – just do it however you feel comfortable.

Task Three

The third task is a modification of the second. It asks you to use a loop instead of writing each variable individually.
The first file, form.php, will be exactly the same as seen in the second task.

submit.php

<?php

foreach( $_POST as $key => $value ) {

echo "$key => $value<br />";

}

?>

Much easier, huh? Using logic like this can shorten code, but it’s not always the best way. With the example above, variables that weren’t user input could be outputted, such as $_POST['submit']. (it wouldn’t in this situation since I didn’t set the name attribute of the submit button). It’s still helpful to try things like this, though. The better problem solving skills that you have, the better.

That concludes the first tutorial. I hope you liked it! The second one is coming soon!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>