DaFudgeWizzad
DaFudgeWizzad

Reputation: 141

How to carry POST variables to another file?

I am working on a form, and want to save a copy of every form submitted. The problem is the form's action is a counting file, that makes each file saved count up once. For example, the third form submitted is named "3.php", and the tenth form submitted is named "10.php". When I try to write the POST variable top the new file, it is gone. Anyway I could write their responses to a new document with my counting files code?

Form code on main file:

<form action="count.php" method="post">
    <input type="text" name="formItem1">
    <input type="text" name="formItem2" required>
    <input type="text" name="formItem4" required>
    <input type="text" name="formItem5" required>
    <input type="text" name="formItem6" required>
    <input type="submit" name="Click" value="Submit">
</form>

Count.php code:

<body>
<?php
define('countlog.txt', __DIR__ . "./");

if (file_exists('countlog.txt')) {
    # If File exists - read its content
    $start = (int) file_get_contents('countlog.txt');
} else {
    # If No File Found - Create new File
    $start =  1;
    @file_put_contents('countlog.txt', "$start");
}

# When Form Submitted
if (isset($_POST) and isset($_POST['Click']) and $_POST['Click'] == "Submit") {
    $file_name = "{$start}.php";
    $template = file_get_contents('template.php');
    @file_put_contents("./submissions/" . $file_name, "$template");
    # Update Counter too
    $start = $start + 1;
    @file_put_contents('countlog.txt', "$start", 1);
    echo "Generated Filename - $file_name";
}
?>
</body>

Template.php code:

echo "<h1>Answer1: " . $formItem1 . "</h1>";
echo "<h1>Answer2: " . $formItem2 . "</h1>";
echo "<h1>Answer3: " . $formItem3 . "</h1>";
echo "<h1>Answer4: " . $formItem4 . "</h1>";
echo "<h1>Answer5: " . $formItem5 . "</h1>";
echo "<h1>Answer: " . $formItem6 . "</h1>";

Upvotes: 1

Views: 1868

Answers (2)

Tristan
Tristan

Reputation: 3321

IF you use $template = file_get_contents('template.php'); it will make the content of template.php a string, and the variables will not be evaluated.

You could use eval() http://php.net/manual/en/function.eval.php to evaluate a string as PHP code. I would not recommend this method, but if you want to use it you will need to return, rather than echo the template otherwise the template will be echoed to the browser rather than the string you want to save to file.

template.php

return "<h1>Answer1: " . $formItem1 . "</h1>".
"<h1>Answer2: " . $formItem2 . "</h1>".
"<h1>Answer3: " . $formItem3 . "</h1>".
"<h1>Answer4: " . $formItem4 . "</h1>".
"<h1>Answer5: " . $formItem5 . "</h1>".
"<h1>Answer: " . $formItem6 . "</h1>";

count.php

$template = file_get_contents('template.php');
$template = eval($template);

It would be much easier/better to include the template, and the code will execute, thus populating the $template variable.

template.php

<?php
$template = "<h1>Answer1: " . $formItem1 . "</h1>".
"<h1>Answer2: " . $formItem2 . "</h1>".
"<h1>Answer3: " . $formItem3 . "</h1>".
"<h1>Answer4: " . $formItem4 . "</h1>".
"<h1>Answer5: " . $formItem5 . "</h1>".
"<h1>Answer: " . $formItem6 . "</h1>";
?>

count.php

include('template.php');

Both methods assume you have used extract($_POST); to import variables from an array into the current symbol table.

Upvotes: 1

Avi
Avi

Reputation: 567

Use sessions. Create a session on each page with session_start(); Store the post value using the session global array. eg. $_SESSION['yourValue'] = POST['yourValue'];. Now on the rest of the pages you should be able to access the value. e.g

$yourValue = $_SESSION['yourValue'];

Upvotes: 1

Related Questions