Diana adam
Diana adam

Reputation: 49

How to save file txt using html and php

i try to save textarea in html to file ("C:\xampp\htdocs\test\1\one.txt")

so i figure out i must use php too.

this php code, it work fine

$fp = fopen('one.txt', 'w');
fwrite($fp, 'edit me please');
fclose($fp);
?>

so how can i use html textarea to edit php 'edit me please' to any text i wanna to edit, plus that i wanna to enable multi line which save in one.txt if i need to.

my html code

<!DOCTYPE html>
<html>
<head>
<title>Using innerHTML</title>
</head>

<body>

your one.txt write in textarea :

<br>
<textarea id="emp" rows = "20" cols = "90" name = "description"></textarea>
<br>

<p>
<input type="button" id="bt" value="Change Label Text" onclick="Onclicky()" />
</p>

</body>



<script language="javascript">
function Onclicky() {

/////function which will do work, idk?!////

}
</script>
</html>

Update #1 (try to add php inside html), but still not work no txt file data created.)

<html>
<body>


<form action="" method="post">
<textarea name="text"></textarea>
<input type="submit" value="edit" />
</form>

<?php
if(isset($_POST["text"]){
$fp = fopen('one.txt', 'w');
if(fwrite($fp, $_POST["text"])){
    echo "Edited";
}
fclose($fp);
}
?>


</body>
</html>

i'm still new, will be glade for your help :)..

Upvotes: 1

Views: 107

Answers (2)

Dev I.A
Dev I.A

Reputation: 643

You Need To know More About "Handle Form Using PHP".

  • $_GET : collect form data.

html :

<form action="your_data.php" method="get">
Enter a Name: 

<br>
<textarea id="emp" rows = "20" cols = "90" name = "name"></textarea>
<br>

<input type="submit">
</form>

</body>
</html>

your_data.php :

<html>
<body>

your data :   
<?php
$fp = fopen('one.txt', 'w');
fwrite($fp, $_GET["name"]);
fclose($fp);
?>

</body>
</html>

to know more!

Upvotes: 2

Dum
Dum

Reputation: 1501

Here is the most easiest way to do it.

<form action="" method="post">
    <textarea name="text"></textarea>
    <input type="submit" value="edit" />
</form>

<?php
if(isset($_POST["text"]){
    $fp = fopen('one.txt', 'w');
    if(fwrite($fp, $_POST["text"])){
        echo "Edited";
    }
    fclose($fp);
}
?>

Upvotes: 1

Related Questions