Welcome to WebmasterWorld Guest from 107.23.176.162

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Fatal error: Call to undefined function validate form()

     
3:13 pm on Feb 7, 2011 (gmt 0)

New User

5+ Year Member

joined:Feb 1, 2011
posts: 8
votes: 0


I am trying to get the form to re-post with error messages in line. Keep getting a fatal error for an undefined function. I can't understand why it is undefined.... it is in the bold line:

else
{
//The request is a POST, so validate the form
$errors = validate_form();
if (count($errors))
{
//If there were errors, redisplay the form with the errors
display_form($errors);
}
}]

/////////////////here is the full code///////////////////

<?php

//Turn on sessions
session_start();

//Find what stage to use
if (($_SERVER['REQUEST_METHOD'] == 'GET') || (!isset($_POST['stage'])))
{
$stage = 1;

}
else
{
$stage = (int) $_POST['stage'];
}

// Save any submitted data
if ($stage > 1)
{
foreach ($_POST as $key => $value)
{
$_SESSION[$key] = $value;
}
}
if ($stage == 1)
{
?>
<?php
if($_SERVER['$_REQUEST_METHOD'] == 'GET')
{
//just display the form if the request is a GET
display_form(array());
}
else
{
//The request is a POST, so validate the form
$errors = validate_form();
if (count($errors))
{
//If there were errors, redisplay the form with the errors
display_form($errors);
}
}

function display_form($errors)
{
//Set up defaults
$defaults['first_name'] = isset($_POST['first_name'])? htmlentities($_POST['first_name']) : '';
}



?>
<form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>

First Name:
<?php print_error('first_name',$errors) ?>
<input type='text' name='first_name' />
<br />
<input type='hidden' name='stage' value='<?php echo $stage + 1 ?>' />
<input type='submit' name='submit' value='Next' />
</form>
<?php

}
else if ($stage == 2)
{
// A helper function to make error message easier
function print_error($key, $errors)
{
if (isset($errors[$key]))
{
print "<dd class='error'>{$errors[$key]}</dd>";
}
}

function validate_form()
{
//Start out with no errors
$errors = array();

//first_name is required and must be at least 2 characters
if (! (isset($_POST['first_name']) && (strln($_POST['first_name']) > 1)))
{
$errors['first_name'] = 'Please enter a name of at least 2 letters.';
}

return $errors;
}

?>
<form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>

Favorite Color:
<input type='text' name='color' />

<input type='hidden' name='stage' value='<?php echo $stage +1 ?>'/>
<input type='submit' value='Done'/>
</form>
<?php

//Validation for the second stage
}
else if ($stage == 3)
{
?>
Hello <?php echo $_SESSION['first_name'] ?>.
Your favorite color is <?php echo $_SESSION['color'] ?>
<?php
}
?>
4:18 pm on Feb 7, 2011 (gmt 0)

Junior Member from IN 

10+ Year Member

joined:Nov 3, 2002
posts: 91
votes: 0


Define functions outside conditional statements.
5:40 pm on Feb 7, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


Right, move your function to just before the very last ?>
3:53 pm on Feb 8, 2011 (gmt 0)

New User

5+ Year Member

joined:Feb 1, 2011
posts: 8
votes: 0


thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you

Spent about a week trying to figure that out....dang it.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members