HTML Form to post on php page
Thank you for reading. I'm trying to create a HTML form so that my friend can type text into it and thereafter, updates his web site with whatever is typed into the form. I'm trying to create a HTML form (on a php page) which posts whatever is entered within it's textarea to the home.php file. However, rather than simply do a "one-off" post, I'm trying to make it so that whatever is entered within the textarea saves the data into the home.php file. The home.php file is blank, and the form which I have created is as below:
<form method="post" action="home.php">
<textarea id="element" name="element" rows="15" cols="80" style="width: 80%">
</textarea>
<input type="submit" name="save" value开发者_如何学JAVA="Save" />
<input type="reset" name="reset" value="Reset" />
</form>
For example, if the words "example" was typed into the form then submitted, the home.php file should have the words "example" written on it.
If you require more details, then please reply. Thank you. :)
<?php
$Input = $_POST['element'];
$FileToUpdate = "home.php";
$fh = fopen($FileToUpdate , 'w') or die("can't open file");
fwrite($fh, $Input);
fclose($fh);
?>
The code above will do what you wish, but will overwrite the page (to append see this reference). But really I think you need to start from basics with a good PHP Tutorial.
This should do what you want:
<?php
$filename = "/path/to/home.php";
$file = fopen( $filename, "w" );
if( $file == false ) {
echo ( "Error in opening new file" );
exit();
}
fwrite( $file, $_POST['element'] );
fclose( $file );
?>
You can read more about file I/O here.
You can use the php $_POST var to fetch the data from a form post.
For example if you want to fetch the field named "element" you can use $_POST['element']
Try the code below to display the text which was typed into the textarea. The code goes into home.php
<?php
echo $_POST['element'];
?>
Likewise you can fetch all required data. Hope this helps. Please go through http://www.w3schools.com/php/php_post.asp for more information.
精彩评论