Gw Temp

Menu

Tutorial - 'Madlib Game' by skyrider

An item about Programming Languages posted on

Blurb

A tutorial that explains how to do those fun little games where you input random parts of speech and get an amusing story.

Body

     Hey, Skyrider again but this time with a tutorial for you on how to make a small madlibs game for your website. This tutorial is probably a LOT smaller than all of my other ones, but this tutorial is also one of the simplest. All you need to know for this tutorial, is how to print variables from a form. Pretty easy, eh? Lets get started.

Part one: Displaying the form
 For this part, we will check to see if the "submit" button has been pressed. If it hasn't then we will show the form and nothing but the form.





<?php
if (!$submit)
//if the submit button wasn't pressed
{
?>
<form action="<? echo $PHP_SELF; ?>" method="POST">
Person:<input type="text" name="name"><br>
Meal:<input type="text" name="meal"><br>
Noun:<input type="text" name="noun1"><br>
Verb:<input type="text" name="verb1"><br>
Verb(Past-tense):<input type="text" name="verb2"><br>
Person:<input type="text" name="name2"><br>
ADJ:<input type="text" name="ADJ1"><br>
ADJ:<input type="text" name="ADJ2"><br>
<input type="reset" name="reset" value="Reset All"><input type="submit" name="submit" value="Put it together!">
</form>


Part two: Displaying the results
 And now it is time to display the results of what you entered above. All we will be doing here is getting the results from the form fields and displaying them in a small paragraph of random text.





<?php
}

else
//if the submit button WAS pressed
{
echo "$name was walking down the street when he came across a(n) $noun1 during $meal. He swung the $noun1 around and then $verb2 on it. He took it home and showed $name2. Later on, $name and $name2 cooked the $noun1 and had it for $meal. It tasted very $ADJ1 but $ADJ2 nonetheless.";
}
?>


Part three: Puting it all together
 Right here, I am just going to put all this code together so you can study its structure, or even copy + paste it.





<?php
if (!$submit)
//if the submit button wasn't pressed
{
?>
<form action="<? echo $PHP_SELF; ?>" method="POST">
Person:<input type="text" name="name"><br>
Meal:<input type="text" name="meal"><br>
Noun:<input type="text" name="noun1"><br>
Verb:<input type="text" name="verb1"><br>
Verb(Past-tense):<input type="text" name="verb2"><br>
Person:<input type="text" name="name2"><br>
ADJ:<input type="text" name="ADJ1"><br>
ADJ:<input type="text" name="ADJ2"><br>
<input type="reset" name="reset" value="Reset All"><input type="submit" name="submit" value="Put it together!">
</form>
<?php
}

else
//if the submit button WAS pressed
{
echo "$name was walking down the street when he came across a(n) $noun1 during $meal. He swung the $noun1 around and then $verb2 on it. He took it home and showed $name2. Later on, $name and $name2 cooked the $noun1 and had it for $meal. It tasted very $ADJ1 but $ADJ2 nonetheless.";
}
?>


 Pretty simple, huh? I would also like you to take note that on most of my tutorials, I use < echo $PHP_SELF; > in the form's ACTION field. That will just refresh the page when the submit button is pressed. And the way I have it, it will show something different than what was shown before. Anyways, enjoy yor new (fun? =\) mad-libs game. Remember to change anything you want different. Also, for an example of a mad-libs game I have made, click here.