Jump to content

Fatal error: Call to undefined function generate_menu() in C:\xampp\htdocs\form.php on line 65


Recommended Posts

please i've been trying to create a registration form for my website and it seems like its taking forever..... i've got some error of which i have tried to rectify but to no avail, so decided post the code and the error to see if someone might be of help to me

 

 

 

<?php
 $required=array("fname" => "Firstname",
	"lname" => "Lastname",
	"email" => "Email address",
	"password" => "Password");
 foreach ($required as $field => $label)
 {
	if (isset($_POST[$field]))
	{
	 $warnings[$field] = "Required";
	}
 }

	if (isset($_POST["email"])&&
!ereg("^[^@]+@([a-z\-]+\.)+[a-z]{2,4}$", $_POST["email"]))
$warnings["email"] = "Invalid email";
if (isset($_POST["telephone"])&&
!ereg("^\([[:digit:]]{3}\)[[:digit:]]{4}-[[:digit:]]{4}$",
$_POST["telephone"]))
$warnings["telephone"] = "Must be (555)0000-0000";

$password=$_POST["password"];
$salt=rand(100000,999999);
	$encrypted= (md5($salt.$password));

if (empty($warnings) > 0)
{

	?>
<form action = "register.php" method=post>
<table border=0>
<tr>
<td>Firstname:</td>
<td><input type=text size=30 name="fname" value="<?php echo (isset($_POST["fname"])? $_POST ["fname"]:"");?>" > </td>
<td><?php echo (isset($warnings["fname"])? $warnings["fname"]:"");?> </td>
</tr><br/>
<tr>
<td>Lastname:</td>
<td><input type=text size=30 name="lname" value="<?php echo (isset($_POST["lname"])? $_POST ["lname"]:"");?>"> </td>
<td><?php echo (isset($warnings["lname"])? $warnings["lname"]:"");?> </td>
</tr>
<tr>
<td>Email address:</td>
<td><input type=text size=30 name="email" <?php if (isset($warnings["email"])) echo "STYLE=\"shaded\"";?> value="<?php echo (isset($_POST["email"])? $_POST["email"]:"");?>"> </td>
<td><?php echo (isset($warnings["email"])? $warnings["email"]:"");?> </td>
</tr>
<tr>
<td>Password:</td>
<td><input type=password size=30 name="password" value="<?php echo (isset($_POST["password"])? $_POST["password"]:"");?>"> </td>
<td><?php echo (isset($warnings["password"])? $warnings[password]:"");?> </td>
</tr>
<tr>
<td>Telephone:</td>
<td><input type=text size=30 name="telephone" value="<?php echo (isset($_POST["telephone"])? $_POST["telephone"]:"")?>"> </td>
<td><?php echo (isset($warning["telephone"])? $warnings["telephone"]:"");?> </td>
</tr>
<tr>
<td>sex:</td>
<td><?php include("sex.php"); ?> </td>
<td></td>
</tr>
<tr>
<td>Date of birth:</td>
<?php
$menu = generate_menu("year", $endYear = '', $startYear = '1900');
function generate_menu($name, $endYear = '', $startYear = '1900' )
{
$selected = "<SELECT NAME=\"$name\">";
	for ( $i = $startYear; $i <= $endYear; $i++ )
	{
	$selected .= "<OPTION ";
	  ( $i == date('Y') ) ?
	$selected .= "selected='selected'" : $selected = '';
	$selected .= "value=\'$i\'>$i</OPTION>";
}
$selected .= "</SELECT>";
return($selected);
}
  ?>
<?  
$month = array(1=> "Jan", 2=> "Feb", 3=> "Mar", 4=> "Apr", 5=> "May", 6=> "Jun", 7=> "Jul", 8=> "Aug", 9=> "Sep", 10=> "Oct", 11=> "Nov", 12=> "Dec");
$default = "1";
$option = generate_menu("month", $month, $default);
function generate_menu($name, $month, $default="")
{
$select = "<SELECT NAME=\"$name\">";
foreach($month as $value => $label)
{
	$select .= "<OPTION ";
	if ($value == $default)
	$select .= "selected='selected'";
	$select .= "value=\'$value\'>$label</OPTION>";
}
$select .= "</SELECT>";
return($select);
}
  ?>
<?
  $function = generate_menu("day");
function generate_menu($name)
{$sel = "<SELECT NAME=\"$name\">";
	 for ( $i = 1; $i <= 31; $i++ )
	 {
	$sel .= "<OPTION ";
	( $i == date('j') ) ?
	$sel .= "selected='selected'" : $sel = '';
	$sel .= "value=\'$i\'>$i</OPTION>";
}
$sel .= "</SELECT>";
return($sel);
}
?>
<td>
<?php print $menu;?>
</td>
<td>
<?php print $option;?>
</td>
<td>
<?php print $function;?>
</td>
</tr>
</TABLE>
<INPUT TYPE=SUBMIT VALUE="Register">
</form>
<?php
}
else {
echo "Thank you for registering";
}
?>

 

 

error

 

 

 

Fatal error: Call to undefined function generate_menu() in C:\xampp\htdocs\form.php on line 65

Edited by stutego

Very quick rundown:

  • Don't do this:
    !ereg ("^[^@]+@([a-z\-]+\.)+[a-z]{2,4}$", $_POST["email"])


    Not only is ereg () deprecated, but you should be using filter_val () with the VALIDATE_EMAIL flag in this case.

  • Same with this:
    ereg ("^\([[:digit:]]{3}\)[[:digit:]]{4}-[[:digit:]]{4}$", $_POST["telephone"])


    Use preg_match () for this one.

  • As for this:
    $salt = rand (100000, 999999);
    $encrypted = (md5 ($salt . $password));
    


    Not only is the salt not quite good enough, and MD5 an hashing algorithm (as opposed to encryption), but using a single-pass MD5 to protect the passwords is almost as bad as saving them in clear text. Use a proper salt, and a secure hashing algorithm like blowfish.
    I strongly recommend that you read this article about secure login systems, and watch

    for more information on this subject.
  • Why are you defining the generate_menu () function three times (in a row to boot)?
    This will cause problems, as PHP does not allow you to overload (redefine) a function.
  • However, since that's not the error message you're getting, I suspect you're doing all of this inside another function. Right?
    If so, please read this PHP manual page.

Edited by Christian F.

Your function definition is inside of a conditional statement, so it is only defined when the conditional statement is true. Under this condition, you must define the function before you can call it.

 

The best method for defining functions is to put them at or near the start of your code and to make them unconditionally defined.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.