Fatal error: Call to undefined function

Yeah, it’s me again…

I’m getting this error:

Fatal error: Call to undefined function form_row_class() in …/test/form.php on line 78

Here is that function which are lines 50-53:


function form_row_class($name){
	global $errors;
	return $errors[$name] ? "form_error_row" : "";
	}

and here is where the error is happening. It’s line 2 here:


<table class="form">
  <tr class="<?php echo form_row_class("first_name") ?>" >
    <td>
    <label for="first_name">First Name*</label>
    </td>
    <td>
    <input name="first_name" id="first_name" type="text" value="<?php echo h($_POST['first_name']); ?>" />
			<?php echo error_for('first_name') ?>
    </td>
    </tr>

I did some research and from what I read it said to check that the function is listed before the script is trying to call it (check). Make sure it’s linked to properly (its all one page). Make sure everything is spelled correctly (check).

What do you experts see, since I’m a newb?

your missing some closing semicolons here

<?php echo form_row_class("first_name") ?>

and here

echo error_for('first_name')

if you have the function with in the scope should work.

I added closing semicolons, but I still receive the fatal error.


<table class="form">
  <tr class="<?php echo form_row_class("first_name"); ?>" >
    <td>
    <label for="first_name">First Name*</label>
    </td>
    <td>
    <input name="first_name" id="first_name" type="text" value="<?php echo h($_POST['first_name']); ?>" />
			<?php echo error_for('first_name'); ?>
    </td>
    </tr>

What you’ve posted seems okay! Could/would you post the complete file?

BTW. If it’s just one statement within <?php and ?>, a semicolon is not required.

I removed the semicolons again since I don’t need them. If you haven’t seen my other thread I’m a noob. This is my third week of my PHP class for school and I have put in about 15 hours of work since Wednesday after I got out of work. The professor isn’t helpful. I e-mailed him my file asking if he could check it out and he replied with “I’ll try to get around to it before Sunday night” which is when this is due. Here is my file… it’s long.


<?php
$title = "Christmas Party";


$errors = array();

if($_SERVER['REQUEST_METHOD']  == 'POST'){

	//validate
	if(0 === preg_match("/\\S+/", $_POST['first_name'])){
	$errors['first_name'] = "Please enter your first name.";
	}
	
	if(0 === preg_match("/\\S+/", $_POST['last_name'])){
	$errors['last_name'] = "Please enter your last name.";
	}
	
	if(0 === preg_match("/\\S+/", $_POST['address'])){
	$errors['address'] = "Please enter your address.";
	}
	
	if(0 === preg_match("/\\S+/", $_POST['city'])){
	$errors['city'] = "Please enter your city.";
	}
	
	if(0 === preg_match("^[A-Z][A-Z]$", $_POST['state'])){
	$errors['first_name'] = "Please enter your state (ex. NY)";
	}
	
	if(0 === preg_match("/^[0-9]{5}$/", $_POST['postal_code'])){
	$errors['postal_code'] = "Please enter your five digit postal code.";
	}
	
	if(0 === preg_match("/^[0-9]{10}$/", $_POST['phone_number'])){
	$errors['phone_number'] = "Please enter your phone number. (ex. 1234567890)";
	}
	
	//IMAGE VALIDATION
 	define ("MAX_SIZE","100"); 

	//This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
 	function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
	}

 	$errors=0;
	//checks if the form has been submitted
 	if(isset($_POST['Submit'])) 
 	{
 	//reads the name of the file the user submitted for uploading
 	$image=$_FILES['image']['name'];
 	//if it is not empty
 	if ($image) 
 	{
 	//get the original name of the file from the clients machine
 		$filename = stripslashes($_FILES['image']['name']);
 	//get the extension of the file in a lower case format
  		$extension = getExtension($filename);
 		$extension = strtolower($extension);
 	//if it is not a known extension, we will suppose it is an error and will not  upload the file,  
	//otherwise we will do more tests
 	if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "pdf")) 
 		{
		//print error message
 			echo 'Only .jpg, .jpeg, and .pdf file format is supported';
 			$errors=1;
 		}
 		else
 		{
	//get the size of the image in bytes
 	//$_FILES['image']['tmp_name'] is the temporary filename of the file
	 //in which the uploaded file was stored on the server
 	$size=filesize($_FILES['image']['tmp_name']);

	//compare the size with the max size we defined and print error if bigger
	if ($size > MAX_SIZE*1024)
	{
	echo 'File is too large.';
	$errors=1;
	}

	//we will give an unique name, for example the time in unix time format
	$image_name=time().'.'.$extension;
	//the new name will be containing the full path where will be stored (images folder)
	$newname="images/".$image_name;
	//we verify if the image has been uploaded, and print error instead
	$copied = copy($_FILES['image']['tmp_name'], $newname);
	if (!$copied) 
	{
	echo 'Upload failed!';
	$errors=1;
	}}}}

	//If no errors registred, print the success message
 	if(isset($_POST['Submit']) && !$errors) 
 	{
 	echo "File Uploaded Successfully!";
 	}
	
	   
	
	//if no errors
	if(0 === count($errors)){
	
	$first_name = mysql_real_escape_string($_POST['first_name']);
	$last_name = mysql_real_escape_string($_POST['last_name']);
	$city = mysql_real_escape_string($_POST['city']);
	$address = mysql_real_escape_string($_POST['address']);
	$state = mysql_real_escape_string($_POST['state']);
	$postal_code = mysql_real_escape_string($_POST['postal_code']);
	$phone_number = mysql_real_escape_string($_POST['phone_number']);
	$rsvp = mysql_real_escape_string($_POST['rsvp']);
	$image = mysql_real_escape_string($_FILES['image']);
	}
	
	//connect to database
mysql_connect ("mverminski.db.3327979.hostedresource.com", "mverminski", "xxxxxxxx")
	or die ('Error: ' . mysql_error());
	
//Insert data into database
$query="INSERT INTO Christmas_Party (ID, first_name, last_name, address, city, state, postal_code, phone_number, rsvp) 
VALUES ('NULL', '$first_name', '$last_name', '$address', '$city', '$state', '$postal_code', '$phone_number', '$rsvp')";

$result = mysql_query($query);

if(mysql_errno() === 0){
//header("Location: update.php");
	echo "Database Updated With: '$first_name', '$last_name', '$address', '$city', '$state', '$postal_code', '$phone_number', '$rsvp', '$image'";
}

//Helpers
function form_row_class($name){
	global $errors;
	return $errors[$name] ? "form_error_row" : "";
	}

function error_for($name){
	global $errors;
	if($errors[$name]){
		return "<div class='form_error'>" . $errors[$name] . "</div>";
		}
	}
		
function h($string){
	return htmlspecialchars($string);
	}

}	
?>
<html>
<head>
<title>Christmas Party RSVP Form</title>
</head>

<body>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
	<h4>Company Christmas Party!</h4>
	
<table class="form">
  <tr class="<?php echo form_row_class("first_name") ?>" >
    <td>
    <label for="first_name">First Name*</label>
    </td>
    <td>
    <input name="first_name" id="first_name" type="text" value="<?php echo h($_POST['first_name']); ?>" />
			<?php echo error_for('first_name') ?>
    </td>
    </tr>
        
  <tr class="<?php echo form_row_class("last_name") ?>" >
  <tr>
    <td>
    <label for="last_name">Last Name*</label>
    </td>
    <td>
    <input name="last_name" id="last_name" type="text" value="<?php echo h($_POST['last_name']); ?>" />
			<?php echo error_for('last_name') ?>
    </td>
  </tr>
  </tr>
    
  <tr class="<?php echo form_row_class("address") ?>" >
  <tr>
    <td>
    <label for="address">Address*</label>
    </td>
    <td>
    <input name="address" id="address" type="text" value="<?php echo h($_POST['address']); ?>" />
			<?php echo error_for('address') ?>
    </td>
  </tr>
  </tr>
  
  <tr class="<?php echo form_row_class("city") ?>" >
  <tr>
    <td>
    <label for="city">City*</label>
    </td>
    <td>
    <input name="city" id="city" type="text" value="<?php echo h($_POST['city']); ?>" />
			<?php echo error_for('city') ?>
    </td>
  </tr>
  </tr> 
  
  <tr class="<?php echo form_row_class("state") ?>" >
  <tr>
    <td>
    <label for="state">State*</label>
    </td>
    <td>
    <input name="state" id="state" type="text" value="<?php echo h($_POST['state']); ?>" />
			<?php echo error_for('state') ?>
    </td>
  </tr>
  </tr>
  
  <tr class="<?php echo form_row_class("postal_code") ?>" >
  <tr>
    <td>
    <label for="postal_code">Postal Code*</label>
    </td>
    <td>
    <input name="postal_code" id="postal_code" type="text" value="<?php echo h($_POST['postal_code']); ?>" />
			<?php echo error_for('postal_code') ?>
    </td>
  </tr>
  </tr>
  
  <tr class="<?php echo form_row_class("phone_number") ?>" >
  <tr>
    <td>
    <label for="phone_number">Phone Number*</label>
    </td>
    <td>
    <input name="phone_number" id="phone_number" type="text" value="<?php echo h($_POST['phone_number']); ?>" />
			<?php echo error_for('phone_number') ?>
    </td>
  </tr>
  </tr>
  
  <tr>
  	<td>
    <label for="RSVP">RSVP*</label>
    </td>
    <td>
    <input name = "rsvp" type = "radio" value = "yes" checked />
    Yes &nbsp;
	<input type = "radio" name = "rsvp" value = "no" />No
    </td>
  </tr>
</table>

<form name="newad" method="post" enctype="multipart/form-data"  action="">
 <table>
 	<tr>
    	<td>
    	<input type="file" name="image">
    	</td>
    </tr>
 	<tr>
    	<td>
        <input name="Submit" type="submit" value="Upload image">
        </td>
   	</tr>
 </table>	
 </form>
 
 <input type="submit" value="Submit"  />


</form>
</body>
</html>

Again, the line giving me a fatal error is line 2 here:


<table class="form">
  <tr class="<?php echo form_row_class("first_name") ?>" >
    <td>
    <label for="first_name">First Name*</label>
    </td>
    <td>
    <input name="first_name" id="first_name" type="text" value="<?php echo h($_POST['first_name']); ?>" />
			<?php echo error_for('first_name') ?>
    </td>
    </tr>

I used the live chat sessions from class, as well as my book, and tons of online resources to get this far, but I feel like using so many resources is causing problems as well. I still have more left to do and I really appreciate any help.

To simplify your code a bit, what you have is


<?php
// ...
if($_SERVER['REQUEST_METHOD']  == 'POST'){
// ...
// validation, etc
// ...

//Helpers
function form_row_class($name){
	global $errors;
	return $errors[$name] ? "form_error_row" : "";
	}

function error_for($name){
	global $errors;
	if($errors[$name]){
		return "<div class='form_error'>" . $errors[$name] . "</div>";
		}
	}

function h($string){
	return htmlspecialchars($string);
	}
}
// ...
?>
<tr class="<?php echo form_row_class("first_name") ?>" >

Indeed, if you haven’t submitted the form (and the method is GET, not POST), the function is not defined.

What you need to do is move the the function from inside the if block to outside the if block:


<?php
// ...
if($_SERVER['REQUEST_METHOD']  == 'POST'){
// ...
// validation, etc
// ...
}
//Helpers
function form_row_class($name){
	global $errors;
	return $errors[$name] ? "form_error_row" : "";
}

function error_for($name){
	global $errors;
	if($errors[$name]){
		return "<div class='form_error'>" . $errors[$name] . "</div>";
	}
}

function h($string){
	return htmlspecialchars($string);
}

?>
<tr class="<?php echo form_row_class("first_name") ?>" >

Does that make sense? :slight_smile:

Thank you! That makes sense and now my page actually displays. I changed line 7 to


if($_SERVER['REQUEST_METHOD']  == 'GET'){

but when I test it and click Submit nothing happens. Is this probably an issue with the submit button/ this?


<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">

Should it be linking to another page, say update.php for example?

No you can (and should) just keep it “POST”, and then move the functions outside that if block, so they are always defined.

But how come when I click submit nothing happens? The request method is POST and the form method is also POST. I click submit and the page doesn’t do a thing.

it’s live if you want to see for yourself, Christmas Party RSVP Form

You have missed the d off method in your form declaration.


<form metho="POST" action="">

Well that was a dumb mistake, but I fixed that and I’m still getting the same result.