Ok I have created a simple form in order to build new product pages using php. I wanted to be able to have a simple form that a data entry person here could input text and such areas as page titles, meta content, brand names, and other text on the new page. The form seems to work fine, however, I would ultimately like the form results page to be an html page. Of course using php scripts to echo the form inputs needs to be coded in a .php file.

Is there a way to submit php forms and have the outputs saved as a new .html page?

I'm trying to make this process as simple and easy to use as I can. The person who will be using this has NO understanding of html or any coding. A form seemed like the most straighforward solution. Thanks for any help!

Recommended Answers

All 7 Replies

I have once done something similar for a website i have worked on. It was for the CMS section. What I wanted to achieve was: Edit a web page's details and save the details directly to an HTML file.
The logic was simple, after clicking the save button on the PHP form, i would read the contents of a pre-existing HTML Template file(using the function file_get_contents() ) into a variable, and then replace the placeholder values (eg __TITLE__, __BODY__) within the variable's contents using the posted information, then finally save the altered variable contents to a new html file (using the function file_put_contents())
I hope this is similar to the solution you need.

Member Avatar

You can use str_replace() or sprintf() functions, to name but a few, to replace placeholders. Regex can also be used for this. wilch has it.

Ok Im really new to php and I didnt understand either post fully. Thanks for the replies though!
As an example, my form page is called newPageForm.php. It has the form and the form action is newPageResults.php.

Ardav, you mentioned something about a function called str_replace()

Could you please elaborate on this function. I have searched for it, but again being so new to php, I dont understand where and how to use this for renaming page extensions themselves.

Let me show you my code. Here is the form:

<form action="newPageResults.php" method="post" enctype="application/x-www-form-urlencoded" name="newproduct">
      <td><input name="metaDescription" type="text" /></td>
      <td><input name="metaKeyword" type="text" /></td>
      <th>Page Title:</th>
      <td><input name="pageTitle" type="text" /></td>
      <th>Product Name:</th>
      <td><input name="productName" type="text" /></td>
      <th>Brand Name 1:</th>
      <td><input name="brandname1" type="text" /></td>
      <td><input type="submit" value="Submit"  /></td>

And here is the results page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description"
content="<?php echo ($_POST['metaDescription']); ?>" />
<meta name="keywords"
content="<?php echo ($_POST['metaKeywords']); ?>" />
<title><?php echo($_POST['pageTitle']); ?></title>
Voltage: <?php echo ($_POST['voltage']); ?><br>
Wattage: <?php echo ($_POST['wattage']); ?><br>
Average Life: <?php echo ($_POST['averageLife']); ?><br>
Glass: <?php echo ($_POST['glass']); ?><br>
Base: <?php echo ($_POST['base']); ?><br>

Disregard the mismatched form names. I just pulled a sample from each page, as there are quite a few more. Thanks again!

Member Avatar

Ardav, you mentioned something about a function called str_replace()

Could you please elaborate on this function. I have searched for it, but again being so new to php, I dont understand where and how to use this for renaming page extensions themselves.

No, I'm sure you can look this up yourself. Download the php manual (chm file).



Place the template file (html file with placeholders, e.g. __TITLE__ into a string [file_get_contents()], like wilch said.

Then, replace bits of the string with your variables, using whichever string manipulation functions you want. Save the modified string to a new file with the name of your choice [file_put_contents()].

If we elaborate more, I think we'll end up writing your code for you.

Well if you view my posts here, you'd see I have never asked someone to write any code for me. Being very new to php, coming from coldfusion, it is very confusing to say the least. I could have written this in coldfusion in a matter of a few minutes, but I AM trying to learn php. I have read about this specific function in the php manual, but still didnt understand some of it with regards to replacing the file extension of a page newly created.

But thanks for your help.

Member Avatar

No prob. Didn't mean to sound shirty. Apologies. The downloadable php manual is an excellent resource and should allow you to research all aspects of what you need.

Ok well 4 days and I still havent figured this out...lol

What I have now is this. And again it may be a completely wrong approach. I have a page called formPage.php that has about 15 form fields on it. Once the submit button is clicked, each form data is inserted into various corresponding html elements in the formResultsPage.php.

All that works great! I now have a new product page. But one issue is its named formResultsPage.php and not the file name I need... ie "newBulb.html"

I have done quite a bit of reading and now think I can use the copy() function to save the formResultsPage.php as a new file like "newBulb.html". However, I dont know where and how to emplement this function, nor do I know how its going to work anyway. The formResultsPage.php must be parsed so that there are no php scripts to even be saved as an html document. Therefore somehow this(the saving part) would have to be done AFTER the page is rendered by the php server.

Please, I need some help, thoughts, suggestions, anything on this matter..lol Thanks again for substantitive replies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.