Functions

So I have a simple form here but I can’t seem to get it to work. The user should enter their name and profession - it then goes to test.php which will store the values in variables in functions so I can reuse the code when the form get bigger but if I can only return one variable how do I get final.php to print out the values in the previous form? I tried to include the functions but no luck I think im missing something major or something simple :confused: - here’s what I got:


<html>
<head>
</head>
<body>

<form action="test.php" method="post">

Name: <input type="text" size="12px" name="name" />
Profession <input type="text" size="12px" name="job" />
	<br />
<input type="submit" />

</form>

</body>


<?php 
	function yourName()
	{
		$name = $_POST['name'];
		$job = $_POST['job'];
		
		$host  = $_SERVER['HTTP_HOST'];
		$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\\\');
		header("Location: http://$host$uri/final.php");
	}
?>


<?php include('test.php'); ?>

<html>
<head>
</head>
<body>

<?php echo $name ?>
<?php echo $job ?>

</body>
</html>

You’re missing something majorly simple. The function call. Nowhere in your code is the yourName() function called. Furthermore, when that function is called, it redirects the browser. That’s going to make it hard to get at your data.

Here’s what I’d do:

  1. Lose the redirect (unless you plan to store the data in a cookie, session or database first)

  2. After your

include('test.php');

add:

yourName();
  1. Set your forms action to go to final.php, which will import test and then run the function.

That way your code will be called and run on the page.

The echo statements you have will return undefined errors because the variables don’t exist within the same scope, an easy solution would be to echo the values out within the function itself but you could go one step further and use a call time by reference which is a bit more tricky.

You can find a good explanation about how to use a call time by reference at the following URL http://stackoverflow.com/questions/2544077/php-call-time-pass-reference?answertab=oldest#tab-top