HTML Form to post on php page

时光总嘲笑我的痴心妄想 提交于 2019-12-20 06:38:34

问题


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="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. :)


回答1:


<?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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/3292684/html-form-to-post-on-php-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!