SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Functions

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 :/ - here's what I got:

    Code:
    <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>
    Code:
    <?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");
    	}
    ?>
    Code:
    <?php include('test.php'); ?>
    
    <html>
    <head>
    </head>
    <body>
    
    <?php echo $name ?>
    <?php echo $job ?>
    
    </body>
    </html>

  2. #2
    SitePoint Addict Zarin Denatrose's Avatar
    Join Date
    Jan 2009
    Location
    Surrey BC, Canada
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Code:
    include('test.php');
    add:
    Code:
    yourName();
    3. 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.

  3. #3
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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/2...oldest#tab-top


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •