Skip to content

PHP Madlib

A simple madlib example.

This PHP Madlib example uses a mix of PHP and markup, with a bit of CSS for presentation.

PHP Madlib Form

There are two pages; one page has a form where a person can enter in words, and the other will display the results.

PHP Madlib

When the submit button (Create story) is clicked, the words entered into the form are sent to another PHP page that uses them to build the madlib story.

<form action="result.php" method="post">
First Name:<input type="text" name="firstname"><br />
Another First Name:<input type="text" name="secondname"><br />
Location:<input type="text" name="location"><br />
Occupation:<input type="text" name="occupation"><br />
Adjective:<input type="text" name="adjective"><br />
Verb:<input type="text" name="verb"><br />
Noun:<input type="text" name="noun"><br />
<input type="submit" name="madlib" value="Create story!">
</form>

PHP Madlib Story

First names used in the story are capitalized (even if the player enters them lowercase).

PHP Madlib

If the player has not entered any words, defaults are used.

PHP Madlib with default values

$firstname='Fearless Frank';
$location = 'Boston';
$secondname= 'Moonbeam';
$adjective = 'fancy';
$verb = 'sleep';
$noun = 'marshmallow';
$occupation = 'rockstar';

if ($_POST['firstname'] != NULL){$firstname = $_POST['firstname'];}
if ($_POST['location'] != NULL){$location = $_POST['location'];}
if ($_POST['secondname'] != NULL){$secondname= $_POST['secondname'];}
if ($_POST['adjective'] != NULL){$adjective = $_POST['adjective'];}
if ($_POST['verb'] != NULL){$verb = $_POST['verb'];}
if ($_POST['noun'] != NULL){$noun = $_POST['noun'];}
if ($_POST['occupation'] != NULL){$occupation = $_POST['occupation'];}

$firstname= ucwords($firstname);
$secondname= ucwords($secondname);

echo "There once was a $occupation named $firstname who lived in $location
and liked to $verb and talk to the neighbor's $adjective $noun. This 
bothered $firstname's best friend $secondname. The end.";

View example

PHP Madlib Code

Full pages with PHP, markup, and CSS.

Index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Madlib!</title>
<style type="text/css">
form {text-align: right;}
	
.madlibbox {       
	background: #F2F1E9;
	border: #25201A 1px solid;
	padding: 10px;
	font-size: medium;
	margin: auto;
	width: 340px;
	font-family: Verdana, "Myriad Web", Syntax, sans-serif;	
        }  

body	{	
	padding: 0;
	border: 0;
	background: #DFDFC8;
	width:	auto;
	text-align: center;
	}		
	
h1 {padding: 10px; text-align: center; text-transform: uppercase;}
</style>
</head>
<body>
<div class = "content">
<h1>Madlib!</h1>

<div class = "madlibbox">
<!--//submission form
choose your own elements (nouns, verbs, etc.) 
to replace these defaults//-->
<form action="result.php" method="post">
First Name:<input type="text" name="firstname"><br />
Another First Name:<input type="text" name="secondname"><br />
Location:<input type="text" name="location"><br />
Occupation:<input type="text" name="occupation"><br />
Adjective:<input type="text" name="adjective"><br />
Verb:<input type="text" name="verb"><br />
Noun:<input type="text" name="noun"><br />
<input type="submit" name="madlib" value="Create story!">
</form>
</div>
</div></body></html>

Results.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php

/*store the sent information in variables*/
$firstname='Fearless Frank';
$location = 'Boston';
$secondname= 'Moonbeam';
$adjective = 'fancy';
$verb = 'sleep';
$noun = 'marshmallow';
$occupation = 'rockstar';

if ($_POST['firstname'] != NULL){$firstname = $_POST['firstname'];}
if ($_POST['location'] != NULL){$location = $_POST['location'];}
if ($_POST['secondname'] != NULL){$secondname= $_POST['secondname'];}
if ($_POST['adjective'] != NULL){$adjective = $_POST['adjective'];}
if ($_POST['verb'] != NULL){$verb = $_POST['verb'];}
if ($_POST['noun'] != NULL){$noun = $_POST['noun'];}
if ($_POST['occupation'] != NULL){$occupation = $_POST['occupation'];}

$firstname= ucwords($firstname);
$secondname= ucwords($secondname);

/*-------------------------------------*/

/*ALTERNATIVE: change your words to uppercase if needed*/
//$firstname= ucwords($_POST['firstname']);
//$location= ucwords($_POST['location']);
//$secondname= ucwords($_POST['secondname']);
/*-------------------------------------*/

/*ALTERNATIVE: bold submitted words*/
//$firstname= "<span class=\"submitted\">".ucwords($_POST['firstname']).
"</span>";
/*-------------------------------------*/

$title="The Story of $firstname";
echo "<title>$title</title>";
?>

<style type="text/css">
form {text-align: right;}
	
.madlibbox {       
	background: #F2F1E9;
	border-right: #25201A 1px solid;
	border-top: #25201A 1px solid;
	border-left: #25201A 1px solid;
	border-bottom: #25201A 1px solid;
	padding: 10px 10px 10px 10px;
	font-size: medium;
	margin: auto;
	width: 340px;
	font-family: Verdana, "Myriad Web", Syntax, sans-serif;	
        }  

body	{	
	padding: 0;
	border: 0;
	background: #DFDFC8;
	width:	auto;
	text-align: center;
	}		
	
h1 {padding: 10px; text-align: center; text-transform: uppercase;}
</style>
</head>
<body>
<div class = "content">
<?php
echo "<h1>$title</h1>";
?>

<div class = "madlibbox">
<?php
/*change the following story - this default story is just an example*/
echo "There once was a $occupation named $firstname who lived in $location 
and liked to $verb and talk to the neighbor's $adjective $noun. This bothered 
$firstname's best friend $secondname. The end.";
/*-------------------------------------*/
?>
</div>
</div></body></html>