1

I have a HTML form that when submitted it goes to a php page which writes a new HTML where the new HTML has the values of the first HTML page written to it.

Ex. In a input:text I put "hi" and hit submit. This then goes to a php page which creates the new HTML where that input:text has value="hi"

The code I have posted below works but I have to put a "dummy" name="x" to get replaced by the value="myvalue". Is there a way to just add this to the element instead of replacing something.

Also is there a way to use this to put the value into a textarea?

Using help from this question: Printing the current page to pdf using wkhtmltopdf I am using this code:

$documentTemplate = file_get_contents ("form.html");

foreach ($_POST as $key => $postVar)
{
if($postVar == "on")
    $documentTemplate = preg_replace ("/name=\"$key\"/", "checked", $documentTemplate);

else
    $documentTemplate = preg_replace ("/name=\"$key\"/", "value=\"$postVar\"",         $documentTemplate);
}

file_put_contents ("form_complete.html", $documentTemplate);

$html = file_get_contents("form_complete.html");

echo $html;
Community
  • 1
  • 1
Brad
  • 65
  • 1
  • 8
  • The usual way this is done is by using the script to create the form in the first place, filling in the values from `$_POST` when they're supplied, rather than using a static HTML file. – Barmar Apr 22 '13 at 20:46

2 Answers2

1

This can be done simply by passing the form's value to somepage.php using either POST or GET - I've used POST in the below example. somepage.php will then check to make sure a value was passed to it, and if a value was indeed passed a form will be generated with an input field containing the post data.

<form action="somepage.php" method="post">
    <input type="text" name="some_field" />
</form>

Somepage.php

if(isset($_POST['some_field'])){
echo '
    <form action="">
        <input type="text" value="' . $_POST['some_field'] . '" />
    </form>';
}
else
{
echo 'post data below: <br>';
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
What have you tried
  • 11,018
  • 4
  • 31
  • 45
1

Instead of using file_get_contents to load all your HTML into a variable, just use your PHP file as an HTML template. Wherever you want to use a PHP variable, add:

<?php echo $somevar; ?>

Make sure to validate that the variable you are echoing into the template exists before rendering it. You may want to put a PHP block at the top of your template that validates and sets all the variables you want to use:

<?php
if(isset($_POST['on'])){
  $on = $_POST['on'];
}
else
{
  $on = '';
}
?>

<html>
  <head>
  </head>
  <body>
    <form action="thispage.php" method="POST">
      <input type="text" name="on" value="<?php echo $on; ?>">
    </form>
  </body>
</html>

You'll find in time that you'll want to separate these into multiple files in which case you can put the HTML into a file like view.php and the validation code into a validate.php then you can just use the require (or require_once) method to include the view at the end of the validator.

There are some common patterns used for separating logic and display. Google MVC.

Chris Hanson
  • 2,043
  • 3
  • 16
  • 26
  • I understand what you mean by this but for my purpose I do want the entire previous HTML to be copied over with the input values. – Brad Apr 23 '13 at 17:11
  • This accomplishes that goal. Use a single PHP page to render the form. When you first run the form, it will be blank because $_POST['on'] is not set so the $on variable gets assigned an empty string. When you submit the form (to itself), the $on variable will get populated by the contents of the POST so the HTML stays the same, but the value changes. – Chris Hanson Apr 23 '13 at 17:17
  • Am I doing something wrong? When I use your code and submit, it just refreshes the page and the input is not saved. – Brad Apr 23 '13 at 17:54
  • I was missing the name in the input box. If you save the code above as "thispage.php" it should do what you're looking for. – Chris Hanson Apr 23 '13 at 17:57
  • Yep that was it. So how do I write the value into a new page, if I use this method, the new page would leave it at instead of whatever the input was. The reason I need the actual file to say it is because of a pdf generating program that I am using. – Brad Apr 23 '13 at 18:06
  • Also with this method I still have to go through and put a unique name and value on every input, I have quite a few inputs, is there a way so I don't have to go through everything and name it or is it a must? – Brad Apr 23 '13 at 18:07
  • There are several tools you could use to persist data. One would be a database, but we've moved beyond the scope of the original question. If you want to get the resulting HTML saved into a file, try the method used here: http://stackoverflow.com/questions/937627/how-to-redirect-stdout-to-a-file-in-php If you want persistent data across page views, use a database. – Chris Hanson Apr 23 '13 at 18:22