I have a small name generator script I am trying to add to my Wordpress website. The script itself works fine, I just don't know how to display it correctly.
It's a simple select form - the user selects a character race and a number of names to generate, and the script pulls from various text files and spits out the names. (Here's a live demo: http://muthaoithcreations.com/name2.php)
<?php
if(isset($_POST['submit']) && !empty($_POST['race']) && !empty($_POST['number'])) // after submitting and all fields are filled, this gets ran and the form below disappears
{
$race = $_POST['race'];
$number = $_POST['number'];
echo "<p>You asked for $number $race name(s):</p>";
$first = explode("
", file_get_contents($race.'first.txt'));
$middle = explode ("
", file_get_contents($race.'middle.txt'));
$last = explode("
", file_get_contents($race.'last.txt'));
$first2 = explode("
", file_get_contents($race.'first.txt'));
$last2 = explode("
", file_get_contents($race.'last.txt'));
shuffle($first);
shuffle($middle);
shuffle($last);
shuffle($first2);
shuffle($last2);
if ($race == "Horc") {
for ($i = 0; $i < $number; $i++) {
echo $first[$i] . $last[$i] . ' ' . $first2[$i] . $last2[$i] . "<br />
"; }
} elseif ($race == "Tiznt") {
for ($i = 0; $i < $number; $i++) {
echo $first[$i] . $middle[$i] . $last[$i] . "<br />
"; }
} else {
for ($i = 0; $i < $number; $i++) {
echo $first[$i] . $last[$i] . "<br />
"; }
}
echo '<p><input type="button" onclick="history.back();" value="Go Back and Try Again!"></p>';
}
else // when the page loads, this else clause gets ran first
{
echo '<p style="color:red;">Please choose your options.</p>';
?>
<form action="name2.php" method="post">
<label for="race">Select Charater Race:</label>
<select name="race" id="race" />
<option value="Oofo" selected>Oofo</option>
<option value="Tiznt">Tizn't</option>
<option value="Werm">Werm</option>
<option value="Horc">Horc</option>
</select>
<label for="number">Names to Generate:</label>
<select name="number" id="number" />
<option value="1" selected>1</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
</select>
<input type="submit" name="submit" value="Give Me Names!"/>
</form>
<?php
}
?>
If I put the generator code into a Page Template and then create a new page using the template, the form displays correctly, but when the user submits the form:
Fatal error: Call to undefined function get_header() in /home/content/09/10184409/html/namegen-page.php on line 7
(The file namegen-page.php is my template file, which is in my theme directory... it appears it's trying to find it in my root directory.)
Do I need to have the form on a different page than the generator? Do I need to use a Wordpress plugin to display the PHP? I'm fairly new to Wordpress, and I'm not sure what I'm doing wrong. Any help is appreciated!