View Full Version : Variable in Include File
mark87
07-24-2005, 06:13 PM
I'm using an include (basically coz it's easier to preview the page), so when a form is submitted the thank you page is included. But how can I pass a variable to the page?
eg.
include("thankyou.html");
Thank you page -
<h1> Thank You " . $firstname . "!</h1>
It just displays 'Thank You " . $firstname . "!' rather than the actual name the person submitted.
Kid Charming
07-24-2005, 06:16 PM
Included files have access to all variables that have been set at the point of the include() call. As long as the var's been set before your include(), it should be fine:
$var = 'myfoo';
include('myinclude.php');
//any instances of $var in myinclude.php will be set to 'myfoo'
Fou-Lu
07-24-2005, 06:19 PM
included() won't work quite as you expect it to. Since the file is not a php file, you'll need to use an eval() function for this to evaluate the php which it contains.
Guess I should do an example, eh?
function parse_file($filename = '')
{
$file_string = file_get_contents($filename);
if (empty($filename) OR $file_string == FALSE)
{
die ('Cannot parse file.');
}
else
{
return addslashes($file_string);
}
}
Include that within a functions script. Please note, this has had no testing and I cannot guarentee its stability. I'd need to test how the escaping is happening to give you something more stable.
// Heres the 'fake' included file:
$firstname = $_POST['firstname'];
eval("\$str = " . parse_file('./thankyou.html') . ";");
echo $str;
and lastly, thankyou.html:
<div>
Thank you for doing whatever, $firstname!
</div>
Or something of the sorts. Try it out, I can create a more detailed example with more script posting from you.
dumpfi
07-24-2005, 11:25 PM
Use this in your included file (assuming $firstname was initialised in the including file):
<h1> Thank You <?php echo $firstname; ?>!</h1>dumpfi
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.