PHP write data from HTML to text file - php

I need to write some data from HTML form to data.txt file (the txt file have 777 permissions)
The HTML that I use is index.html:
<form action="index.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
And the PHP file is index.php:
<?php
$txt = "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
}
?>
But when I use the form to save the data, it show me a message box to download the index.php
what I am doing wrong?

Related

How can I get the output for form without the page changing

I am trying to get information from textarea to be converted to a .py file.
Here's my file https://jsfiddle.net/girlwhocancode/68mb49gq/
<form action=action.php method="post">
<center>
<textarea placeholder="Code you want to execute in python..."></textarea>
</center><br/>
<center><input type="submit" class="button_example"></center>
</form>
and this is my php file:
<?php
$path = #PATH;
if (isset($_POST['field1'])) {
$fh = fopen($path,"a+");
$string = $_POST['field1'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
For some reason my php isn't working and I have no idea how to keep the same webpage after submitting
Try this:
<form action="action.php" method="post">
<center>
<textarea name="script" placeholder="Code you want to execute in python..."></textarea> //added name
</center><br/>
<center><input name="submit" type="submit" class="button_example"></center>
</form> // added name submit
in php
<?php
$path = #PATH;
if (isset($_POST['submit'])) {
$fh = fopen($path,"a+");
$string = $_POST['script'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
Remember that in php $_POST/$_GET keys are always your form field
names

How to pass variable one file to another using f write function

Hi how to pass variables from one file to another file with f write
i have a file name a.php
code is
<?php
if(isset($_POST['psub'])){
$myfile = fopen("an.php", "w") or die("Unable to open file!");
$pst = $_POST['pst'];
fwrite($myfile,$pst);
fclose($myfile);
}
?>
<form action="" method="post">
<input type="text" name="pst">
<input type="submit" name="psub" value="submit">
</form>
i need in an.php
$abc = "GET VALUE FROM $pst"
It looks like you approach this the wrong way.
Add an.php as an action in your form in a.php and, if you need to, redirect back to it after processing needed data.
Your a.php:
<form action="an.php" method="post">
<input type="text" name="pst">
<input type="submit" name="psub" value="submit">
</form>
and your an.php:
if( isset($_POST['psub']) && isset($_POST['pst']) ){
// do what you need with it
$abc = "GET VALUE FROM $_POST['pst']";
}
// some other work that you need
header("Location : a.php");

save post from file into msword

i want to save a post from an html form into a msword file
<form method="post" action="process.php">
<text name="comment"></text>
<input type="submit" name="send" value="submit"/>
</form>
process.php
<?php
if(isset($_POST['submit']))
{
$body=$_POST['comment'];
$newfile = fopen("essay/last.docx", "w") ;
$content = "$body";
fwrite($newfile, $content);
fclose($newfile);
}
?>
the created file is not a valid msword file. any help is appreciated

Simple HTML/PHP Refuses to work

Here is poster dot php
<?php
$FILENAME = "poster.php";
$txt = "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both
fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
}
?>
Here is my HTML form
<!DOCTYPE html>
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="poster.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>
I tried everything to make this work when i put it in a debugger it says the file should end on the first line. When I run the file all i get is a white screen. I'm in desperate need of help.
You are not writing anything after appending your form data in your poster.php file. You can get the file contents in the following way...
Html form:
<!DOCTYPE html>
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="poster.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>
and in poster.php
<?php
$FILENAME = "poster.php";
$txt = "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both
//fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'].PHP_EOL;
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
echo file_get_contents("data.txt");
}
?>
As per requirement, write PHP code and HTML code inside a single file and remove the action attribute of the form in the following code ...
<?php
//$FILENAME = "poster.php";
$txt = "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both
//fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'].PHP_EOL ;
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
//echo file_get_contents("data.txt");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="" 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 attribute of form should be file name where you submit your form.
<form action="action.php" method="post">
Here it is 'action.php' but you have to submit it on poster dot php.
please check it:
<form action="poster.php" method="post">
Form action will be your main file name(poster.php) and after form post, you can easily get all the form fields data on poster.php file.
you wotre this file name -> poster dot php means poster.php ?

Writing text to .txt file using PHP

So I have page.txt which looks like this:
<textarea name="msg" rows="1"></textarea>
<form id="post" name="post" action="storeText.php" method="post">
<input class="button" type="submit" value="Set news now"/>
</form>
I also have storeText.php which looks like this:
<?php
$filename = 'posts.txt';
$msg = (isset($_POST['msg']) ? $_POST['msg'] : null);
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $msg) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($msg) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
Now it should write the text onto posts.txt but it always says "Success, wrote () to file...". Notice the empty parenthesis which should have my input.
Any ideas? Thanks!
<textarea name="msg" rows="1"></textarea>
<form id="post" name="post" action="storeText.php" method="post">
<input class="button" type="submit" value="Set news now"/>
</form>
If the textarea is outside of the form block it won't be sent. So your html should look like:
<form id="post" name="post" action="storeText.php" method="post">
<textarea name="msg" rows="1"></textarea>
<input class="button" type="submit" value="Set news now"/>
</form>
Your <textarea> element is outside of your <form> tags.
<textarea>should either go inside the form tags or you need to specify a form attribute to link the input to a form e.g. <textarea form="myForm"> (the latter may not work in older browsers, though, best to put it inside the <form> tag if you can).
That's why your $_POST['msg'] is null

Categories