I have written a program in php,html. I have created a form and I can save the form inputs in a .txt file.The thing is I need to write a code where I can edit my .txt file inputs and save them again with the changes. Can you help me out? This is the code I have written.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="bug.php">
<label>Emri produktit:</label>
<input type="text" name="emriprod" id="emriprod">
<label>Versioni</label>
<input type="text" name="versioni" id="versioni">
<label>Frekuenca</label>
<input type="text" name="frekuenca" id="frekuenca">
<input type="submit" value="Submit">
</form>
</body>
</html>
bug.php
<?php
$emriprod=$_POST["emriprod"];
$versioni=$_POST["versioni"];
$frekuenca=$_POST["frekuenca"];
$fp=fopen("bug.txt","a");
$string=$emriprod." ".$versioni." ".$frekuenca."\r\n";
fwrite($fp, $string);
fclose($fp);
echo "Te dhenat u ruajten ne file";
?>
You can store the data with special characters to separate the fields .
$string = $emriprod."##".$versioni."##".$frekuenca."\r\n";
Retrieving the data
$file_data = file_get_contents('bug.txt');
$data = $explode('##', $file_data); //separating the string to array
After you editing the existing data, update the file like below
file_put_contents('bug.txt',str_replace('old data','new_data',file_get_contents('bug.txt')));
Hope this helps to solve your problem.
Related
I have tried curl on html from but show error
curl -d user="Kirk" http://xxxxxxx.site/form.php
but in return
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body></html>
I've also tried adding a user-agent, but I'm getting the same error.
i want to store value inside the "user"(html form element) to text file(stored on webserver) using command line. i am able to create this using web-based interface but i want it using command line.
my code for web-based interface
html:
<!DOCTYPE html>
<html>
<head>
<title>Store form data in .txt file</title>
</head>
<body>
<form action="data.php" method="post">
Enter Your Text Here:<br>
<input type="text" name="textdata"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
php:
<?php
if(isset($_POST['textdata']))
{
$data=$_POST['textdata'];
$fp = fopen('log.txt', 'a');
fwrite($fp, $data);
fclose($fp);
}
?>
How do i save data from a HTML form on a server?
Here is my code:
<form action="?action=save" name="myform" method="post">
<textarea class="textBox" name="mytext"> </textarea>
<input type="submit" class="save" value="save"/>
Thanks in advance.
You will need to change the action name to point to where the PHP code will be. I put my PHP code on the same page as my HTML and changed the action to save.php (that's my file name where the PHP is).
This is my save.php file where everything is.
<?php
// Check if form is submitted and we have the fields we want.
if(isset($_POST["mytext"]))
{
$file = "data.txt";
$text = $_POST["mytext"];
// This file will create a data.txt file and put whatever is in the POST field mytext into the text and put a new line on the end.
// The FILE_APPEND allows you to append text to the file. LOCK_EX prevents anyone else from writing to the file at the same time.
file_put_contents($file, $text . "\r\n", FILE_APPEND | LOCK_EX);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Save POST Data</title>
</head>
<body>
<form action="save.php" name="myform" method="post">
<textarea class="textBox" name="mytext"></textarea>
<input type="submit" class="save" value="save"/>
</body>
</html>
Easiest way its set action on save.php and in this file put
<?php
if(isset($_POST)){
file_put_contents('file.txt', json_encode($_POST));
}
You can handle the submit event by js, and send the data to server by using Ajax or fetch. Then in your server side, build an API to catch the request and store data in database or any file you want to store your data.
Best
I have a very basic PHP file. i want to have two textboxes for user input, and a submit button. The user will enter their first and last name, then i would like to append or create a TXT file with the data entered from field1 and field2.
Possibly i am going about this the wrong way. I will post two of the ways i have been tinkering around with.
<html>
<head>
<title>Field1 & 2</title>
</head>
<body>
<form>
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
<?php
$txt= $_POST['field1'].' - '.$_POST['field2'];
$var_str3 = var_export($txt, true); //is this necessary?
$var3 = "$var_str3"; //is this necessary?
file_put_contents('fields.txt', $var3.PHP_EOL, FILE_APPEND);
?>
</body>
</html>
I cant figure out how to get the data from field1 and field2 into a string variable.
I have also messed around with using this php instead of the section listed above
<?php
$txt= "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) {
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the 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
}
?>
Let's take a snippet from http://www.w3schools.com/html/html_forms.asp
<form action="action_page.php" method="post">
First name:<br>
<input type="text" name="firstname" value=""><br>
Last name:<br>
<input type="text" name="lastname" value=""><br><br>
<input type="submit" value="Submit">
</form>
Note the first line: upon form submission a php script is called: action_page.php.
action_page.php is your webpage with the form and the embedded php script. action_page.php both displays the empty form and then process the submitted data.
On the first line also it is specified that the submitted data is sent with the POST method.
The php part will look like this:
<?php
if( isset($_POST['firstname'] ) && isset( $_POST['lastname'] ) )
{
$txt= $_POST['firstname'].' - '.$_POST['lastname'] . PHP_EOL;
file_put_contents('fields.txt', $txt, FILE_APPEND);
}
?>
The if statement is there because the first time the script action_page.php is loaded its purpose is only to display the form and don't receive any POST data.
As the form is submitted by the user the script will receive the data and store to file.
The script will also (with this approach) display again an empty form ready for the submission of another entry.
You can rearrange things in order to have two web pages: one with just the form, another one with a "Thank you" message and the data processing php script.
First of all this is not my code, I just need it changed a little.
I need to know how to write messages onto the same file where the user posts the message.
Here is one page where the user can post their message:
<html>
<head>
<title>Form to Flat File</title>
</head>
<body>
<form action="sendinfo.php" method="get">
Your Name:<br />
<input type="text" name="name"><br />
Your Message:<br />
<textarea name="message"></textarea><br />
<input type="submit" value="Send Info">
</form>
</body>
</html>
And here is the other that writes the message onto a PHP file:
<html>
<head>
<title>Form to Flat File</title>
</head>
<body>
<?php
include('config.php');
$user = $_GET["name"];
$message = $_GET["message"];
print("<b>Thank You!</b><br />Your information has been added! You can see it by <a href=savedinfo.php>Clicking Here</a>");
$out = fopen("savedinfo.php", "a");
if (!$out) {
print("Could not append to file");
exit;
}
fputs ($out,implode,("\n"));
fwrite($out,"<b>$user</b><br />$message<br /><br />");
fclose($out);
?>
</body>
</html>
Pretty much I just want everything on one page, I got close to doing that but it won't let me write onto the same page. I'm sure it's possible I'm just nowhere near experienced enough. Please help!
What you want is to put content of both files in a single file, but execute the content of the second file only if form has been submitted.
First of all, change the form's method value to POST and replace all references to $_GET with $_POST
Then create a single file containing
<html>
<head>
<title>Form to Flat File</title>
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// here put content of the file that stores form values in a file
// just remove all html code and leave only PHP code
}
?>
// here put content of the file that displays the form
// just remove HTML, HEAD and BODY tags first
</body>
</html>
I'm trying to write a program where the basic idea is I ask the user for input in a textarea, and then the text gets stored into a word file. Here is the code I'm trying to use:
<html>
<head>
<title>Simple Guestbook</title>
</head>
<body>
<h1>Simple Guestbook Comment Creator</h1>
<br>
<form method = "post"
action = "mysite.php">
<textarea name = "text"
rows = "10"
cols = "20">Write Here</textarea>
<input type = "submit"
value = "Submit Comment">
</form>
<?
if($_POST['text'] !== NULL){
$comment = $_POST['text'];
$file = fopen("texttest.txt", "a");
fputs($file, "<br>\n$comment");
fclose($file);
}
?>
</body>
</html>
I can't seem to get this to work properly. I was also thinking about somehow making the form action store the text and then reload the site, but I haven't gotten that to work (the original file is mysite.php, so the action is to just reload the page).
If anyone has any better ideas of an algorithm to use/different syntax to use, please let me know, as I just started learning basic PHP syntax.
Thanks
Check the following:
Does php have the permission to write files in that directory?
Is that php file called "myfile.php"?
Anyway, when something does not work and you want to know what's causing the arror, place error_reporting(-1); at the beginning of your php - it will output any error or warning, including the ones trown by fopen().
Also, you might want to check whether the variable has been correctly submitted: echo $comment right after you assign it.
Something like this might work.
You might want to do more with the values they are entering and all, but this will basically do what you are asking.
You will also want to make sure that you have the correct path of the file you are trying to write to and that that file has the correct permissions to allow it to be written to:
<html>
<head>
<title>Simple Guestbook</title>
</head>
<body>
<h1>Simple Guestbook Comment Creator</h1><br>
<?php
if (isset($_POST['submit'])) {
if (strlen(trim($_POST['comment']))) {
$file = fopen("texttest.txt", "a");
fputs($file, "$_POST['comment'])\n");
fclose($file);
}
} else {
?>
<form method = "post" action = "<?php echo($_SERVER['PHP_SELF']); ?>">
<label>Leave your comment
<textarea name="comment" rows="10" cols="20"></textarea>
</label>
<input type="submit" name="submit" value="Submit Comment" />
</form>
<?php
}
?>
</body>
Also, since you are returning to the same page you may want to put some kind of message letting the person know that they succeeded in entering something into your address book.