Get textbox text - php

When I click the button the text from the textbox should be written to a .txt file, which then should be downloaded. Somehow the isset function doesn't work, I already tried to link the php file with a <form>, but then I can't read the textbox text.
Here's my code:
<?PHP
if(isset($_POST['submit']))
{
$text = $_POST['text'];
print ($text);
$filename = 'test.txt';
$string = $text;
$fp = fopen($filename, "w");
fwrite($fp, $string);
fclose($fp);
header('Content-disposition: attachment; filename=test.txt');
header('Content-type: application/txt');
readfile('test.txt');
}
?>
<html>
<head>
<title>Text Editor</title>
</head>
<body>
<textarea name="text" rows="20" cols="100"></textarea>
<p>
<button type="submit" value="submit">Download Text</button>
</body>
</html>

Firstly, you need to have a <form> tag to be able to submit your data:
<body>
<form action="add your php filename here" method="post">
...
</form>
</body>
You might also need to make your <button type="submit" into <input type="submit"

Add a form tag and post back to same page if php document is within the same file.
<?php
if(isset($_POST['submit']))
{
$text = $_POST['text'];
print ($text);
$filename = 'test.txt';
$string = $text;
$fp = fopen($filename, "w");
fwrite($fp, $string);
fclose($fp);
header('Content-disposition: attachment; filename=test.txt');
header('Content-type: application/txt');
readfile('test.txt');
}?>
<html>
<head>
<title>Text Editor</title>
</head>
<body>
<form method="post">
<textarea name="text" rows="20" cols="100"></textarea><p>
<input type="submit" value="submit">Download Text</button>
</form>
</body>
</html>

In your current HTML, you need a <form> tag.
Try reading about this here.

You haven't added the form TaG in HTML.

Related

Forcing a new line when using php to write to a .txt document

I am using an html form, and some php script to write data to a .txt document.
It works fine, but I would like to start a new line with each entry.
I have experimented with some code suggestion, such as '\r\n' and 'nl2br'.
But I believe I am doing it wrong. This is where I left off...
<!DOCTYPE html>
<html>
<head>
</head>
<body >
<form method="POST">
<input type="text" name="textdata" id="textdata">
<input type="submit" name="Submit">
</form>
<script>
<?php
if(isset($_POST['textdata']))
{
$data=$_POST['\r\n' . 'textdata'];
$fp = fopen('data.txt', 'a');
fwrite ($fp, $data);
fclose($fp);
}
?>
</script>
</body>
</html>
You are actually doing wrong. Use post request data as it is.
if(isset($_POST['textdata'])){
$data= "\n" . $_POST['textdata'];
$fp = fopen('data.txt', 'a');
fwrite ($fp, $data);
fclose($fp);
}

Download user input in textarea using HTML and PHP?

I'm working on learning PHP and I'm trying to essentially have the user enter input into two different text areas, and having the content downloaded into a single .txt file once they click the button. I know my code is extremely rough, I haven't found any solutions online for my specific case-scenario, but here it is:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="./style.css" />
</head>
<div class="navbar">
Home
Resume
Projects
Contact
Social
Logout
</div>
<body>
<label for="textbox">Professional Summary</label>
<textarea id="textbox"></textarea>
<label for="bio">Brief Biography</label>
<textarea id="bio"></textarea>
<input type="button" id="homebt" value="Submit" />
</body>
</html>
<?php
if (isset($_POST['submit'])) {
$text = $_POST['bio'];
print ($text);
$filename = 'index.txt';
$string = $text;
$fp = fopen($filename, "w");
fwrite($fp, $string);
fclose($fp);
header('Content-disposition: attachment; filename=test.txt');
header('Content-type: application/txt');
readfile('test.txt');
die;
}
Currently nothing happens when the button is clicked. The entire code is in a file called adminIndex.php, and I have Apache Web Server running to open the file in my browser for testing purposes. If anyone could potentially help me out with what I'm doing wrong, I'd really appreciate it!
Edit (made the following changes to the end of the HTML code):
<form name='form' method='post'>
<label for="textbox">Professional Summary</label>
<textarea id="textbox"></textarea>
<label for="bio">Brief Biography</label>
<textarea id="bio"></textarea>
<input type="submit" id="homebt" value="Submit" name="submit" />
</form>
</body>

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 ?

How to upload input to a text file in php

I want to upload the contents written in the text box in a text file using php. But also they should only be written if the string contains a particular pattern.
here's my php code
<?php
$data = $_POST["text"];
public function copy(){
if (strpos($data, '123') !== false){
$myfile = "uploads/mydata.txt";
$fh = fopen($myfile, 'w') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
}
}
?>
It says unexpected public on line 4.
and my form is:
<?php require_once('tally.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>
</title>
</head>
<body>
<form action="index.php" method="post">
Input the first record
<input type="text" name="text">
<input type="submit" name="submit" action="submit">
Submit
</submit>
</form>
</body>
</html>
Thanks for helping
why are you using fopen, just use file_put_contents.
The input name is text and in php you are trying to receive $_POST['data'].
Change name of input to data or change the php code and get $_POST['text'].
also your submit button is not xhtml or html5 standard compliant. change it to no need for closing tag or additional text.
I cannot see where are you calling the copy function.
code should be something like this.
<?php
$data = $_POST["data"];
if(!empty($data)) {
copy2($data);
}
function copy2($data){
if (strpos($data, '123') !== false){
$myfile = "uploads/mydata.txt";
$fh = fopen($myfile, 'w') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
}
}
?>
HTML goes like this:
<?php require_once('tally.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>
</title>
</head>
<body>
<form action="index.php" method="post">
Input the first record
<input type="text" name="text">
<input type="submit" value="submit">
</form>
</body>
</html>
Remove the word public if this function is not in a class.
public function copy(){
becomes
function copy(){
There is not tag associated with data. Please add:
<input type="text" name="data">

PHP not printing special characters

I'm receiving a text file upload and then just printing it back out. For some reason, special characters are appearing as black boxes with a white check mark. I tried htmlentities() and utf8_encode() on the content to be print to screen, but that didn't help.
Here's all of my code:
<?php ini_set("auto_detect_line_endings", true);
header('Content-Type: text/html; charset=utf-8');
?><!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body style="overflow:visible;">
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit" name="upload" value="upload">Upload</button>
</form>
<pre>
<?php
if($_POST['upload']) {
//$fileName = 'old.txt';
$fileName = $_FILES['file']['tmp_name'];
if(file_exists($fileName)) {
$file = fopen($fileName,'r');
while(!feof($file)) {
$name = fgets($file);
echo(htmlentities($name));
}
fclose($file);
}
}
?>
</pre>
</body>
</html>
This code works on my localhost LAMP server, but the character probelm appears on some other people's servers. What can I do to maek the special characters show up?

Categories