Page 1 of 1

passing data around

Posted: Mon Oct 27, 2008 12:14 am
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.

Re: passing data around

Posted: Mon Oct 27, 2008 12:23 am
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>

Re: passing data around

Posted: Tue Oct 28, 2008 7:30 am
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?

Re: passing data around

Posted: Tue Oct 28, 2008 7:50 am
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');