i posted on this earlier and with forum help get my function code to work as I want when I test it.
But when I insert it into the php document I only get errors.
The function is:

<?php
function fix_it($name)
{
$name=ucwords(strtolower($name));
echo $name;
}
?>

The document form:

<?php //query to select all
if (isset($_POST['name'])) $name = $_POST ['name'];
else $name = "(Not entered)";
echo <<<_END
<html>
	<body>		
	<form method="post" action="allaunch_2.php">
		Enter Ship Name
		<input type="text" name="name" size="25" maxlength ="35"/>
		<input type="image" name="submit" src="newindex_files/cooltext497898955.png" onmouseover="this.src='images/cooltext497898955MouseOver.png';" onmouseout="this.src='images/cooltext497898955.png';" alt="search"/>
	</form>	
	</body>
	</html>
_END;
if($name == "")
{
echo "<p><font color='red' size='4px'>You have not entered a name</font></p>";
exit;
}
else 
{
echo "<p><font color='blue' size='4px'>You searched for:</font></p>";
echo "<p><font color='blue' size='6px'>$name</font></p>";
}

require_once "dbconnect.php";
require_once "fix_it.php";
echo fix_it($name);

//query and table follow
The output is:

Enter Ship Name

You searched for:

miami  //  as input & displays in blue

( ! ) Fatal error: Call to undefined function fix_it() in C:\wamp\www\WSRU\allaunch_2.php on line 26
Call Stack
#	Time	Memory	Function	Location
1	0.0007	394160	{main}( )	..\allaunch_2.php:0

Sorry to make it so long.
Where do I place the function call ?

Member Avatar for diafol

As a rule you wouldn't place the handling code on the same page as the form itself as this has repercussions with regard to the back button and refreshing the page causing a resubmit of the same data. However, if you are going to do this - place all your php functions and as much code as possible above the doctype declaration.

<?php
...functions (e.g. fix_it() and applied code...
...save output to variables as opposed to echoing directly...
?>
<!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>
...head stuff...
</head>
<body>
...forms etc...
<?php echo fix_it($name);?>
</body>
</html>

That may be a little crude, but you get the general idea. BUT as posted on the last thread, we usually place functions in include files so that they're portable and can be used in many pages without having to duplicate code. Better still, when you get more experienced, you can start with Object Orientated Programming, which encourages good practice with regard to non-duplication.

you can put your function inside a class before you call it and instantiate it to your file. like

<?php
class className{
  function fix_it($name){
      $name=ucwords(strtolower($name));
      echo $name; 
   }
}
?>

<html>
<head>
--some code here ----
</head>
<body>
-----forms here------
<?php 
  echo className.fix_it($name);
?>
</body>
</html>
<?php
function fix_it($name)
{
    $name=ucwords(strtolower($name));
    //echo $name; <----Don't echo just return it
    return $name;
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.