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">
Related
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);
}
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 ?
I am facing the following issue. I have a simple textarea where user will use to submit text which is subsequently written to a text file in the server. This is working.
But when I refresh the page it adds in the last added text into the text file again causing duplicate entries.
Any idea what I must do to prevent this? Below is the code I used for the textarea portion.
<html>
<body>
<form name="form" method="post">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit" />
</form>
</body>
</html>
<?php
if(isset($_POST['text_box'])) {
$a = $_POST['text_box'];
$myFile = "textfile.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $a."\r\n");
fclose($fh);
}
?>
Pages that are loaded via POST will cause the browser to ask the user to resubmit the information to view the page resulting in the actions performed by that page happening again. If the pages is requested via GET and has variables in the querystring the same thing happens but silently (without the user being prompted to d it again).
The best to work around this is to use the POST/REDIRECT/GET pattern. I used it in an example about processing payments that I wrote for Authorize.Net. Hopefully that points you in the right direction.
A simpler so
You can just store a simple hash on session and regenerate it every time.
When the user reloads the page the php wont be executed.
<?php
if(isset($_POST['text_box']) && $_SESSION['formFix'] == $_POST['fix']) {
$a = $_POST['text_box'];
$myFile = "textfile.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $a."\r\n");
fclose($fh);
}
?>
<html>
<body>
<form name="form" method="post">
<input type="text" name="text_box" size="50"/>
<?php
$value = md5(rand(1,999999));
$_SESSION['formFix'] = $value;
?>
<input type="hidden" name="fix" value="<?= $value; ?>" />
<input type="submit" id="search-submit" value="submit" />
</form>
</body>
</html>
ps: the order of the blocks will matter, so you need to invert em.
As John said, you need to redirect user after form submit.
fclose($fh);
// and
header("Location: success.php or where else");
exit;
Note: Your redirection won't work unless ob_start is not called before, cos your page contains html outputs.
// form.php
<?php ob_start(); ?>
<html>
<body>
<? if (isset($_GET['success'])): ?>
Submit OK! New submit
<? else: ?>
<form name="form" method="post" action="form.php">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit" />
</form>
<? endif; ?>
</body>
</html>
<?php
if(isset($_POST['text_box'])) {
$a = $_POST['text_box'];
$myFile = "textfile.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $a."\r\n");
fclose($fh);
// send user
header("Location: form.php?success=1");
exit;
}
?>
My code for testing is as follows:
Both files are .php to avoid conflict, I have been programming for 30 years, but am new to .js and .php, I can't figure out the syntax for what should be an easy effort. I have read and tried all applicable examples but they haven't worked for me. PLEASE TELL ME WHERE I'M SCREWING UP!!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>TestofTopicText</title>
<script language="Javascript">
<!--
function OnButton1()
{
var newtopic = document.getElementById('topic');
document.Form1.target = "_self";
document.Form1.action = "1-open-close.php?var=$newtopic";
document.Form1.submit(); // Submit the page
}
-->
</script>
</head>
<body>
<h3><span style="color: #00ff00;">If NOT found to right ENTER Your Topic Here! </span></h3>
// Using get method as I read was appropriate for getElementById
<form id="Form1" method="get" name="Form1">
<input type="text" name="q" id="topic" size="55" />
<input type="submit" name="sa" value="Search" onclick="OnButton1()"/>
</form>
</body>
</html>
// I am passing to this .php file known as 1-open-close.php
// The file opens and writes test text but I can't get topic text from other file?
<?php
$topic = $_GET['var'];
$myFile = "Topics.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "Test to Make Sure Open \n";
fwrite($fh, $topic);
fwrite($fh, $stringData);
fclose($fh);
// return true;
?>
Not sure if you are really looking to involve javascript in this, but you could simplify by just using the form behavior native to html, and reference the input passed in. i.e. You've already specified the form as method GET, so the input type text contained within the form tags will be posted without any extra effort. On the php side, you can reference that value coming through by using the "name" specified on the html input as the index to the array. Hope this helps!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>TestofTopicText</title>
</head>
<body>
<h3><span style="color: #00ff00;">If NOT found to right ENTER Your Topic Here! </span></h3>
// Using get method as I read was appropriate for getElementById
<form id="Form1" method="get" name="Form1" action="1-open-close.php">
<input type="text" name="q" id="topic" size="55" />
<input type="submit" name="sa" value="Search"/>
</form>
</body>
</html>
// I am passing to this .php file known as 1-open-close.php
// The file opens and writes test text but I can't get topic text from other file?
<?php
$topic = $_GET['q'];
$myFile = "Topics.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "Test to Make Sure Open \n";
fwrite($fh, $topic);
fwrite($fh, $stringData);
fclose($fh);
// return true;
?>
JavaScript doesn't interpolate variables like PHP does, so it is literally sending the string $newtopic instead of what you want. The whole thing is redundant anyway. Try this:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TestofTopicText</title>
</head>
<body>
<h3 style="color:#0f0;">If NOT found to right ENTER your topic here!</h3>
<form method="post" action="1-open-close.php">
<input type="text" name="q" size="55" />
<input type="submit" value="Search" />
</form>
</body>
</html>
Then your PHP file should be:
<?php
$topic = $_POST['q'];
$myFile = "Topics.txt";
$fh = fopen($myFile,"a") or die("can't open file");
$stringData = "Test to make sure open\n";
fwrite($fh,$topic);
fwrite($fh,$stringData);
fclose($fh);
?>
EDIT: I see you put your form action into your javascript. That's weird. Anyway:
I suggest you use a LOT of echo statements to find out what variables' values are.
Useful things:
print_r($_POST); // find out what values have been received by POST method
print_r($_SESSION); // find out what values are stored in SESSION
Surround those print_r with <pre></pre> tags to make them easily readable.
Here:
var newtopic = document.getElementById('topic');
document.Form1.target = "_self";
document.Form1.action = "1-open-close.php?var=$newtopic";
You are trying to pass the JavaScript variable newtopic with $newtopic (which is the syntax for PHP variables, and does not apply here).
And you are also trying to send the element itself, which does not make sense.
The input box has a name="q" and that is what you should be accessing in PHP.
Nevertheless the code is still badly formed. You will get errors from trying to access "q" before it is sent.
I've added an action to the form, and deleted your script which was not necessary. I've also changed $topic = $_GET['var'] to $topic = $_GET['q'];
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>TestofTopicText</title>
</head>
<body>
<h3><span style="color: #00ff00;">If NOT found to right ENTER Your Topic Here! </span></h3>
// Using get method as I read was appropriate for getElementById
<form id="Form1" method="get" name="Form1" action="1-open-close.php">
<input type="text" name="q" id="topic" size="55" />
<input type="submit" name="sa" value="Search" />
</form>
</body>
</html>
// I am passing to this .php file known as 1-open-close.php
// The file opens and writes test text but I can't get topic text from other file?
<?php
$topic = $_GET['q'];
$myFile = "Topics.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "Test to Make Sure Open \n";
fwrite($fh, $topic);
fwrite($fh, $stringData);
fclose($fh);
// return true;
?>
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.