Chelsea Cerame
Chelsea Cerame

Reputation: 35

Creating a webpage with data sent from a form

I am trying to build a form in HTML that, once submitted, automatically generates a new webpage using data entered into the form.I'm usually a MATLAB and python user, so I tried them first. Matlab would parse the parse the data and save it to a new .txt file, but not a .html file. Python was much the same. After searching, I came across the suggestion to use PHP to create the new page from the form data. (Someone was using php to create user webpages with the users name, email, and a picture. I tried to adapt this to suit my needs, but it is not generating the new page as I thought it would. Instead, it just displays part of the PHP code. This is the form I made:

<form action="htmlData.php" method="post"> 

Product Name: 
<input name="Name" size="20" type="text">
<br><br>

Project Lead Name:
<input name="PLname" size="20" type="text"> <br><br>

Team-members: <br>
<textarea name="Team_members" rows=10 cols=40> </textarea> <br><br>

Product Type: <br>
<input name="Product_Type" size="20" type="text"> <br><br>

Description: <br>
<textarea name="Description" rows=10 cols=40 type="text"> </textarea>
<br>

<br> <br>



<input value="Submit" type="submit" name="formSubmit">
<input value="Reset" type="reset">
<input value="Help" type="button" onclick="window.location.href='problems.html'">
</form>

...And this is the PHP file named htmlData.php:

ob_start();  
$Name = $_POST['Name'];
$PLname= $_POST['PLname'];
$Team_members= $_POST['Team_members'];
$Product_type= $_POST['Product_type'];
$Description= $_POST['Description']; 
$html = <<<HEREDOC
     Product Name: $Name <br>
     Project Lead: $PLname <br>
     Team Members: $Team_members <br> <br>
     Product Type: $Product_type <br>
     Description: $Description
     HEREDOC;  
file_put_contents('newPage.htm', $html); 
header()redirect.header('location: newPage.html')

What do I need to change so that once a user clicks submit, a new page is generated from the data and the user is then taken to the newly created page? Is this possible with what I have, or should I looking into using a different language?

Upvotes: 1

Views: 3486

Answers (6)

elo
elo

Reputation: 615

  1. If you not need to create new page, in start of your PHP code, remove ob_start(), and add

if(isset($_REQUEST['formSubmit'])){ .... .... }

your page will be created automaticly on submit press.

Upvotes: 0

m.edmondson
m.edmondson

Reputation: 30892

Instead, it just displays part of the PHP code

Seems like one of the following:

  • Have you installed PHP on your server?
  • Is the server configured to pass .php pages to be processed?

It might be worthwile to take a look here as displaying PHP code on the browser means the webserver isn't dealing with it correctly.

Once you think it's installed run phpinfo() see the docs.

Upvotes: 1

TDBishop
TDBishop

Reputation: 84

While not exactly solving your original problem, I hope I'm not too out of bounds with this:

Your problem is most often solved by using a database in conjunction with a language like PHP. Storing this information in a database after form submit will allow newPage.htm you have to accept GET parameters and dynamically display the corresponding data from the database. Example: newPage.html?id=245 will deliver that ID to the PHP code processing that request. PHP will use that ID to query your database for the proper information and display that to the page. This way you will not have one file for each form submission and the form data can easier to maintain. Check out something like: http://www.freewebmasterhelp.com/tutorials/phpmysql for more information.

Another aspect to keep in mind is security. Your current code puts direct user input into a file on your server. This is a huge security vulnerability. It is recommended you escape the user input.

Upvotes: 0

ewein
ewein

Reputation: 2735

You are missing an 'l' in your new file declaration:

file_put_contents('newPage.htm', $html);

Also, try physically creating the newPage.html file and place it in your project directory then see if the page is displayed or not. If this is the case I would check to see if you have PHP installed and if the server is configured to handle post. Usually it is configured to handle post or get but not usually both by default.

Upvotes: 0

vgaldikas
vgaldikas

Reputation: 106

If it shows a php code, that means that: 1. You dont have php installed, 2. Your http server like Apache installed.

Even if the 2 above conditions were met, the code you have is not a valid PHP code, and it would not work.

You are not yet good enough with PHP, you need to go back and learn the very basics before you try make any dynamic pages.

Upvotes: 1

drew010
drew010

Reputation: 69967

It's possible with PHP and Python (perhaps MATLAB too).

Given your PHP code, there are a few small issues. First, the HEREDOC must be ended at the beginning of the line (there can't be any whitespace before the end HEREDOC).

Second, the PHP code to redirect is invalid. Try these changes:

<?php

$Name = $_POST['Name'];
$PLname= $_POST['PLname'];
$Team_members= $_POST['Team_members'];
$Product_type= $_POST['Product_type'];
$Description= $_POST['Description'];

$html = <<<HEREDOC
     Product Name: $Name <br>
     Project Lead: $PLname <br>
     Team Members: $Team_members <br> <br>
     Product Type: $Product_type <br>
     Description: $Description
HEREDOC;  
file_put_contents('newPage.htm', $html); 

header('location: newPage.html');
exit;

The next issue you may encounter is that it cannot write to newPage.htm. You may need/want to specify a full path (e.g. /home/yoursite/public_html/files/newPage.htm).

You will need to make sure that directory is writable by the web server (as often the web server runs as a different user than your files are owned by).

Hope that helps.

Upvotes: 1

Related Questions