PHP Write File from Input to Txt

PHP write file from input to txt

Your form should look like this :

<form action="myprocessingscript.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>

and the PHP

<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "\r\n";
$ret = file_put_contents('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}

I wrote to /tmp/mydata.txt because this way I know exactly where it is. using data.txt writes to that file in the current working directory which I know nothing of in your example.

file_put_contents opens, writes and closes files for you. Don't mess with it.

Further reading:
file_put_contents

Really Simple Php Form To Save To A Text File

Following code should work for you:

<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "\n";
$filename = date('YmdHis').".txt";
$ret = file_put_contents($filename, $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
?>

You can create file dynamically and check if its exist just write your content or create new.

Save html form data to a txt file using php

This is most likely a permission issue. Check write permission with is_writable or enable error output with ini_set('display_errors', true);

Change permissions for data.txt accordingly.

Write text from a form into a text file

This $_Post in $_Post["WMessage"]; must be in uppercase, it's a superglobal.

http://php.net/manual/en/language.variables.superglobals.php

$_POST["WMessage"];

Write form input to .txt file with php?

The issue is that you are using this $email=$_POST("email");
but you have to use this

 $email=$_POST["email"];

I think Below will help you as par ur comment.
for doing without page load user ajax.

<?php
if(isset($_POST["submit"]))
{


$fileHandle = fopen('emailList.txt', 'w+')
OR die ("Can't open file\n");
$email=$_POST["email"];
$result = fwrite ($fileHandle, $email);
if ($result)
{
print '<script type="text/javascript">';
print 'alert("Email added!")';
print '</script>';
} else {
print '<script type="text/javascript">';
print 'alert("Email not added!")';
print '</script>';
};
fclose($fileHandle);
}
?>

<form action="#" method="post">
<input type="text" name="email" class="emailSubmitSidebar" placeholder=" Your Email">
<input type="submit" name="submit" class="submitButton">
</form>

Receive JSON in PHP and save input as TXT file

The following code works

$request = file_get_contents('php://input');
$input = json_decode($request,true);

$firstName = $input['first_name];

$text = print_r($firstName,true);
file_put_contents('output.txt', var_export($text, TRUE));

How do I save form data in HTML to a txt file.

Tested, works 100%. You don't have to create .txt. Gets created automatically if not present.

index.html

<form action="program.php" method="post">
Channel 8 Title:<br><input type="text" name="channel0Title" value="Channel 7"><br>
Gain:<br><input type="text" name="channel0Gain" value="4.000"><br>
Offset:<br><input type="text" name="channel0Offset" value= "6.000"><br>
<input type="submit" id ="submitButton" value="Submit">
</form>

program.php

<?php
$title = $_POST["channel0Title"]; //You have to get the form data
$gain = $_POST["channel0Gain"];
$offset = $_POST["channel0Offset"];
$file = fopen('configurationSettings.txt', 'w+'); //Open your .txt file
ftruncate($file, 0); //Clear the file to 0bit
$content = $title. PHP_EOL .$gain. PHP_EOL .$offset;
fwrite($file , $content); //Now lets write it in there
fclose($file ); //Finally close our .txt
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>

How to save string entered in HTML form to text file

You should learn about HTML Forms And PHP Form Handling.

In your code you have to use a form HTTP method. And the form data must sent for processing to a PHP file.

In this code i use HTTP PSOT method you can also use GET method the result will be same. This two method is used for collecting the form data. And the php file name is "action.php".

index.html

  <html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="action.php" method="post">
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

action.php

<?php
$path = 'data.txt';
if (isset($_POST['field1']) && isset($_POST['field2'])) {
$fh = fopen($path,"a+");
$string = $_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>


Related Topics



Leave a reply



Submit