passing data around

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
grose
Forum Newbie
Posts: 4
Joined: Thu Oct 16, 2008 9:11 pm

passing data around

Post by grose »

I've been trying to work out how to pass data around between php files. The idea being data from a form would go to a controller for processing then passed to a view page for display. The practice example that I made for myself has three pages: first.php, second.php and you guessed it, third.php
first.php sends the selected item to second.php

Code: Select all

 
<form action='second.php' method="post">
            <select name="sltTopic">
                        <option>A</option>
                        <option>B</option>
                        <option>C</option>
            </select>
            <input type="submit" value="Enter" />
        </form>
 
second.php takes the data sends it on to third.php. I realise there is no commands to send the data on, this is where I am clueless.

Code: Select all

 
<?php
    $option = $_POST['sltTopic'];
?>
 
third.php displays the data.

Code: Select all

 
<h1>$option</h1>
 
In first.php I would use the forms 'action' element, triggered by submit, to send the data to second.php but I don't know how to send from second.php to third.php.

Also what is this called? I tried searching for an answer but it has been fruitless as I am uncertain of the terminology.

Thanks in advance.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: passing data around

Post by s.dot »

You can just pass the value directly to the view. You will only need 2 files. And usually, passing data to a view requires a template engine. So the page that calls the view would look something like:

Code: Select all

//get the post value
$myValue = $_POST['myValue'];
 
//this assigns the value to the template
$tpl->assign('myData', $myValue);
 
//this displays the template page
$tpl->display('mytemplate.tpl');
And your template would look something like this:

Code: Select all

<h1>{ $myData }</h1>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
grose
Forum Newbie
Posts: 4
Joined: Thu Oct 16, 2008 9:11 pm

Re: passing data around

Post by grose »

Thanks scottayy for replying.

Though using a templating engine would work, doesn't basic php provide a way of passing data to another page?

first.php can send data to second via POST.
third.php can get data from second via include('second.php').
So how do I get from second.php to third.php? I would need some sort of function in second.php so that I can get third displayed.

I can add include('third.php') in second.php which would give the same result as sending the data from second to third but for this experiment I am trying to learn how to send data between pages. Isn't there a way in plain old php to do this?
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: passing data around

Post by watson516 »

First off, your options don't have any values so they wont pass anything.

If you include the third.php into the second.php, it would be just like writing the code from third.php into second.php so its not really passing anything.

You could use GET.

Code: Select all

 
...
header('Location: http://www.example.com/third.php?variable=value');
 
Post Reply