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?
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.
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.
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.
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.
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.
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.
Related
I have a form on my website. I wanted to save to input to txt but when I submit data, the .txt file get larger (bytes increase) but no text shows up,
Here is the code
<div>
<form action="controller.php">
<input name="card" id="card" type="email">
<button type="submit" name="submit" >Submit</button>
</form>
</div>
Here is the .php code
<?php
$card = $_POST['card'];
$file = fopen ('file.txt', "a");
fwrite($file, $card . "\n");
fclose($file);
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>
I have a sub domain with the same code and it works perfectly fine.
why is it not working and how do I fix?
I tried to change the id and the type
You have a very simple issue here. Let's see why by trying to do this on your file:
print_r($_POST);
You will see that after posting your form, you have no POST data. Your form is, by default (on your server or PHP configuration) sending the data not using the POST method, but the GET one (your other server probably is setup the opposite way).
To fix this, you can either change your $card variable to:
$card = $_GET['card'];
Or rather, and that would be a better option to make it more clear and avoid problems if you migrate your website on another server/PHP version, you could simply specify the method on your tag:
<form action="controller.php" method="post">
Please, don't forget to secure the data that will be written in this file, malicious users exist and if you keep your code that simple, it might be a serious security issue.
I've started delving into PHP and I've seen that PHP files have a ".php" ending. I was wondering how this works, if you can put HTML code into one of these files, and how one would usually use PHP to collect information, if they'd create a separate web page for the form or if they can just convert an HTML page into a PHP page.
Apologies if my question is a bit hard to understand, it's a bit hard to put to words what I want to know.
Thanks!
You can put html in php files, for example:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
This file file will display on a browser the classic Hello World page!
The way to use php depends of what you're trying to do. Sometimes it will be mixed with html, sometimes no.
Whenever a webserver sees a page ending in .php and has a PHP interpreter registered then it will pass the page to the interpreter.
The interpreter will pass any result back. If there's no PHP code in the page (e.g. there's only HTML) the interpreter will just pass the HTML back the way it gets it, but if you add a <?php ... ?> block the interpreter will evaluate the contents of that block and replace it with the result (e.g. if you do <?php echo "Hello world" ?> it will replace it with "Hello world").
That's just the short version.
PHP and HTML would be used in conjuction to create web pages. One can convert a HTML document to PHP in order to acheive more functionality as they are used for different things. PHP is a server side scripting language i.e. it facilitates communication between the user interface (the HTML & CSS) and the back-end server stuff (data). HTML is a markup language which on it's own does not communicate with the server, it is simply used to display static content on a page.
Together, they can be used to create dynamic web pages. One way to think of it is like this: HTML provides the content whereas PHP manipulates the content in order to provide functionality.
In other words, if we were building a house then HTML would be the bricks and mortar and PHP the plumbing, heating and electricity.
You can include HTML code in a PHP file however all PHP scripts must start with:
<?php
and end with
?>
Regarding your question on collecting information, PHP provides functions which enable form creation and binding to databases, take the following:
<?php
echo '<form method="post" action="">
Username: <input type="text" name="user_name" /><br/>
Password: <input type="password" name="user_pass"><br/>
Password again: <input type="password" name="user_pass_check"><br/>
E-mail: <input type="email" name="user_email">
<input type="submit" value="Register" />
</form>';
?>
This is an example of a simple form written in PHP to enable a user to log in, where HTML and PHP differ is that while you can create the same form in HTML, PHP gives you access to all sorts of nifty features such as performing data validation, security checks and database access.
I am having trouble extracting data from a form. I would like to save email address to a text file to the local server. The user will go firstly to this site (picture),
website
They will type in their email address and a wireless key (controlled by Unifi) and then hit submit. At that point, I would like the email they entered to be saved to a text file. The connect button also authenticates the user with Unifi and they are able to connect to the guest wireless.
The code I have so far is:
<!-- Email Input -->
<fieldset class="large-text">
<p class="form-element">
<label for="email" class="fieldname">Email</label>
<input id="emailcontainer" method="post" name="emailtxtbox" class="textInput" value="" autocomplete="on" input type="email" required placeholder="Enter your email"/>
</p>
</fieldset>
<!-- End of Email Input !-->
This creates the email textbox entry that you see above in the picture.
The below code is the php execute, that will (hopefully) grab the email, and then save it too the text file:
$getemail = $_GET["emailtxtbox"];
$emailtxtbox = $_POST["emailtxtbox"];
if($getemail = "connect") {
$targetFolder = "/emailcollate";
file_put_contents($targetFolder."mytext.txt", $emailtxtbox);
}
As you might of guessed, I have no skill in php or html, so please keep it simple. I have played around the above codes a lot, so some of it may have different names etc. I would appreciate any help!
Does anyone think it would be better to use the file_put_contents() function to do the above?
ok, it has been a long time since i have worked with php, but i will do my best.
the way I would troubleshoot this is make sure the php page actually ran, easily done with:
var_dump("this page ran");
after the page has run, dump the two variables out to the page to confirm that the page has received them and has accurately change them to a variable.
next, in your if statement, dump some random text to make sure the if statement has run.
otherwise, make sure you know where the php file is trying to save to, may have to use this instead:
file_put_contents("/emailcollate/mytext.txt", $emailtxtbox);
the file_put_contents() function should work fine
In PHP $GET is used for picking up parameters in your site's URL, and $POST is used for gathering form values that are submitted to your page.
In your case you're getting the value for $getemail from the URL of your page, so for $getemail to have a value the URL for your site should resemble
www.yoursite.com/?emailtxtbox=connect
Additionally you need to add an extra equals sign in your if statement, like so:
if($getemail == "connect") {
The double equals is a comparator used for comparing two values against each other, rather than a single equals which is used for assigning a value to a variable.
Your file_put_contents looks ok, but like user6063785 has suggested try running that block of code outside of the if statement to see if the file is being written to. You may need to ensure that PHP has permissions to write to the file.
I've been trying to use PHP to allow a user to put in articles onto a certain site via a form.
The site,my code and my skills as a programmer are primitive in nature.
The user enters the input here:
<form action="articlepro.php" method="post">
Title:<input type="text" name="title"><br>
Sub-Heading:<input type="text" name="subhead"><br>
Intro:<textarea name="intro" rows="10" cols="40"></textarea><br>
Heading-1:<input type="text" name="head1"><br>
Content-1:<textarea name="cont1" rows="10" cols="40"></textarea><br>
<input type="submit">
</form>
The date is processed as such by the file articepro.php:
<html>
<body>
<?php
$title=$_POST["title"] . ".php";
$createFile=fopen($title,"x+");
$content=file_get_contents("articletemplate.php");
echo $createFile;
echo file_put_contents($title,$content);
echo $createFile;
?>
</body>
</html>
The aim of the code above is to first create a variable that stores the title from the form with the .php extension.Then the code creates a file with the same name as the $title variable.$content stores the html and php code of the file "articletemplate.php" as a string and "file put contents" puts that template into the file created earlier.
It may be of of value to know that artictemplate.php contains the template I wish for all new articles to have with bits of php that put in data as such:
<p><h1><?php echo $_POST["head1"];?></h1>
<?php echo $_POST["cont1"];?>
</p>
Now the problem I face is the fact that my code both creates the required files with the required title(Permisson:644),it also contains the intended template.BUT,I am unable to display anything when I open the created files(.php's) via URL.I either see a blank page or "k????????????????????????????????????????????????????????????????????????????????????????ä???????????????ä??"
I can open them if I download the created files off the server and change the extension to .html(cant use PHP then) though but no luck with .php or .phtml.
Webserver:Apache/2.2.24 (Unix
I'm new to web development and have been wrestling with this problem for several hours now, so I've decided to turn to your wisdom. I'm trying to design a little webpage with a database for my wife to store her recipes in, but I'm having trouble getting form submission to work. Here is the code for the webpage where I take the form information in:
<html><body>
Enter the information below to add a new ingredient for use in your recipes.
<form action="add_to_database.php" method="post">
Name: <input name="name" type="text" />
Serving: <input type="text" name="serving" />
Calories: <input type="text" name="calories" />
<input type="submit" />
</form>
</body></html>
And here is some silly code I've been trying to display on the page to see if I can even get the form submission to work:
<html><body>
<?php
$name = $_POST['name'];
echo $name."<br />";
?>
</body></html>
Unfortunately, the page comes back as completely back (after I hit the submit button). What's absolutely baffling to me is that I've copied and pasted the examples from this page into some files and everything seems to work fine. So it seems as though apache and php are working correctly, but I'm messing up somewhere along the way. My apologies in advance if this seems like a stupid question but for the life of me I can't figure it out.
Do you name the other file as add_to_database.php where the form is submitted. Instead you can test on teh same page by removing the add_to_database.php from the form action.
form action="" method="post">
Name: <input name="name" type="text" />
Serving: <input type="text" name="serving" />
Calories: <input type="text" name="calories" />
<input type="submit" />
</form>
and write the php code on the same page as
$name = $_POST['name'];
echo $name;
If this is not working for you. Create a php file named test.php and type phpinfo(); there.
Verify that your page is indeed being processed by PHP - obvious question, but does your PHP file have a .php extension? Do other things like phpinfo() or echo "Test"; work?
Check the error log in /var/log/apache2/error.log or similar (if on Linux, dunno where that'd be on Windows).
Try turning display_errors on in the PHP configuration (this is a good idea only for a development install, not a production server).
a bit of a longshot, but if you can verify that php is pro essing the page. Clean up your html, you are missing the dtd, head, and encoding. . hard to say how a browser is going to interpret a form (missing a name attribute) without these.