Reputation: 45
I have this PHP file, which will write the text to a file:
<?php
$file = 'something.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new line to the file
$current .= "Some Text\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>
But how do I make it so it takes the text from a input tag instead? I am not very good at PHP, so sorry if it is obvious to you :(
Upvotes: 0
Views: 69
Reputation: 60
html:
<form action="example.php">
<input type="text" name="field">
<input type="submit" value="Submit">
</form>
PHP:
<?php
if (isset($_GET['field'])) {
$file = 'something.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new line to the file
$current .= "\n".$_GET['field'];
// Write the contents back to the file
file_put_contents($file, $current);
}
?>
or you can use the append flag mentioned in the previous comments.
Upvotes: 2
Reputation: 165
Assume you have a form in your webpage:
<form action="write.php" method="post">
<input type="text" name="field">
<input type="submit" value="Submit">
</form>
Then in your "write.php" should have this:
<?php
$file = 'something.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new line to the file
$current .= $_POST["field"];
$current .= "\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>
Upvotes: 0