Question

I have the large form and all data written by user are treated in a special way. My form after submitting should load template php file and add data from form into it. So my app treats POST data, loads the php template through file_get_contents() and throught fwrite() writes data into the new php file.

But here comes the problem. The variables, which were in the php template file, are written as they are. But I need to replace variables from php template by their values from submitted and parsed form POST header.

Does anybody know, how to do it?

My simplified code:

-- form.php
<Form Action="process.php" Method="post">
<Input Name="Name1" Type="text" Value="Value1">
<Button Type="submit">Submit</Button>

-- process.php
$Array=array(
"Name1","Name2",//...
);
if(!empty($_POST)){
foreach($Array as $Value){
if(!empty($_POST[$Value])){
$Value=$_POST[$Value];
}}}
...
$Template=file_get_contents("template.php");
$File=fopen("../export/".$userid.".html","w+");
fwrite($File,$Template);
fclose($File);

-- template.php
<!Doctype Html>
...
Name1: <?=$Name1?><Br>
...

My goal:

-- 135462.html
<!Doctype Html>
...
Name1: Value1
...
Was it helpful?

Solution

I think you are looking for php buffers. ob_* will help you do that.

Check http://php.net/ob_start

template.php :

<html>
<head></head>
<body><?=$foo?></body>
</html>

index.php :

<?php
$foo = $_POST['text'];
ob_start();
include('template.php');
$template_html = ob_get_contents();
ob_end_clean();

//do your stuff

echo $template_html;
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top