I have issue saving textarea to file. I used POST method to send the form to the other page then, in the next page I can't include the textarea content with the file Im not sure what is the problem.
Is there any idea about what is the problem?
Here are the two pages:
page1:
<!DOCTYPE HTML>
<html>
<head>
<title>Save</title>
</head>
<body>
<form action="page2.php" method="post">
<span>name:</span>
<input type="text" name="name"><br>
<span>file extension: </span>
<select name="ext" id="ext">
<option value=".txt">.txt</option>
<option value=".doc">.doc</option>
</select>
<textarea name="txt1" id="txt1" cols="15" rows="10"></textarea>
<br>
<input type="submit" name="submit" id="submit" value="Save">
</form>
<br>
</body>
</html>
-page2.php
$txt1 = $_POST['txt1']; //textarea
$name = $_POST['name'];
$ext = $_POST['ext']; //choose from multiple extensions
if ($ext == '.txt') // In case if I want to add more than extension.
{
$file = "'. $name$ext.'" ;
$output = "$txt1";
file_put_contents($file, $output);
$text = file_get_contents($file);
header("Content-Description: File Transfer");
header("Content-Type: application/text/plain");
header("Content-Disposition: attachment; filename=".basename($file));
ob_clean();
flush();
readfile($file);
exit;
}
Without seeing your html I can't be sure of what the problem is. But its been my experience that when your having trouble accessing POST vars on the server side that it's probably a simple spelling error. Make sure the name attributes in your form line up with your POST vars. Just my two cents.
I have no idea what did you mean with your code, so, I'd just rewrite it
To save a file on the server you need these 2 lines
$name = basename($_POST['name']).'.txt';
file_put_contents($name, $_POST['txt1']);
$file = "'. $name$ext.'";
should be:
$file = $name.$ext;
At least that's what I had to change to get it to work on my server.
You need to add an id to the form, then add the form Id to the textarea element. For example:
<form action="page2.php" method="post" id="myform">
<textarea name="txt1" id="txt1" cols="15" rows="10" form="myform"></textarea>
Try using the wrap element in your textarea
<textarea name="txt1" id="txt1" cols="15" rows="10"></textarea>
add wrap
<textarea name="txt1" id="txt1" cols="15" rows="10" wrap="virtual"></textarea>
you can also use wrap: off, hard, soft and physical
In your database make sure the field txt1 is defined properly (i.e type text).
Related
I am trying to make a button that saves form data on a file and redirects you afterwards, but although the save function works just fine, the redirection doesn't happen. I've tried only href and formaction so far. Any suggestions?
Paste bin : pastebin
Thank you for your time reading this !
the code:
<html>
<head></head>
<footer>
<form method="post">
<input type="radio" id="html" name="fav_language" value="private_number" checked="checked">
<label for="html">private_number</label><br>
<input type="radio" id="css" name="fav_language" value="clieant_number">
<label for="css">clieant_number</label><br>
<input type="text" id="fname" name="fname" maxlength="11" autocomplete="off" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1');" required >
<button type="submit" name="BUY"><i class="arrow right"></i></button>
</form>
</footer>
<?php
$numurs = $_POST['fname'];
$Partners = $_POST['fav_language'];
if ($numurs){
$f = fopen('numurs.csv', 'w');
fputcsv($f, Array($numurs, $Partners));
fclose($f);
}
?>
</body>
</html>
You can use PHP's header() function for this. Change the PHP part to this
<?php
$numurs = $_POST['fname'];
$Partners = $_POST['fav_language'];
if ($numurs){
$f = fopen('numurs.csv', 'w');
fputcsv($f, Array($numurs, $Partners));
fclose($f);
header('Location: '.$_SERVER['REQUEST_URI']);
exit;
}
?>
My php page has the HTML and PHP all on the same page.
For the HTML section, user data is inputted and posted to the PHP section.
</div>
</header>
<pre>
</pre>
<h1 style="font-family: 'Lucida Sans Typewriter'"> Note entry:</h1>
<form method="post">
<div>
<div style="margin-right:5px;">
<input type="text" class="fileName" name="fileName" id="fileName" size="35"
value="untitled">
<input type="text" name="noteData" id="notes" size="250">
<input type="Submit" name="Submit" value="Save">
</div>
</div>
</form>
As for the PHP section, I check to see if a POST has occured, then save file and filename to disk, yet this does not occur.
if($_POST){
$noteName = $_POST['fileName'];
$noteData = $_POST['notes'];
$notes = fopen('' + $noteName,"wb");
fwrite($notes,$noteData);
fclose($notes);
}
The errors thrown are here:
https://gyazo.com/99646e2705ed7ea927d27c67cddb87b4
Any suggestions? I need this finished soon and cannot for the life of me discover why this is deciding not to work.
You have to use names to get data using $_POST. And use . to concatenate words in php. So change the line as:
if($_POST){
$noteName = $_POST['fileName'];
$noteData = $_POST['noteData'];
$notes = fopen('' . $noteName,"wb");
fwrite($notes,$noteData);
fclose($notes);
}
You should use names, not ID. Try $_POST[‘noteData’].
I am trying to build a website people can fill/upload necessary information in a form. Based on these information, the server does some computation, generates an output file and return the file. Everything goes well except the last step of returning. Following is a simplified version of my site that you can test directly. In this example, it should return a file containing the file name uploaded by the user while stay on the same page. However, it actually returns the html code of the page and the file name. What should I do to get only the output file not the html code? Thanks a lot!
I tried with flask, and everything went well. Now, for some reason I would like to translate everything to php. I am really new to website building and lack a lot of background knowledge.
<!DOCTYPE html>
<html>
<body>
<form action="" method="POST" autocomplete="on"
enctype="multipart/form-data">
<div> Upload your file: <input type="file" name="file"/> </div>
<input style="margin-left: 0.5em;" type="submit" id="click"
value="Click Me!" name='submit_btn'/>
</form>
<?php
if(isset($_POST['submit_btn']))
{
$fp = fopen("./output.txt", "w");
fwrite($fp, $_FILES['file']['name']."\n");
fclose($fp);
header("Content-Type:text/plain");
header("Content-Type: application/download");
header('Content-Disposition: attachment;
filename="output.txt"');
readfile("output.txt");
}
?>
</html>
You're trying to return both HTML and a file. Each HTTP request can result in only one or the other. Separate your concerns. Start with a PHP resource which only returns the file you posted to it:
<?php
if(isset($_POST['submit_btn']))
{
$fp = fopen("./output.txt", "w");
fwrite($fp, $_FILES['file']['name']."\n");
fclose($fp);
header("Content-Type:text/plain");
header("Content-Type: application/download");
header('Content-Disposition: attachment;
filename="output.txt"');
readfile("output.txt");
}
?>
Then write an HTML page which posts to that PHP resource:
<!DOCTYPE html>
<html>
<body>
<form action="yourPHPFileAbove.php" method="POST" autocomplete="on"
enctype="multipart/form-data">
<div> Upload your file: <input type="file" name="file"/> </div>
<input style="margin-left: 0.5em;" type="submit" id="click"
value="Click Me!" name='submit_btn'/>
</form>
</body>
</html>
Note specifically how the action attribute in the <form> posts to a specific PHP resource, not just back to itself.
Edit: Just to clarify, I only want to open a file and change some text.
I have a basic HTML page with a form, and I want to change the POST action programatically with PHP.
I have this PHP script which I got from another post:
<?php
$file = file_get_contents($argv[1]);
$startPoint='action="';
$endPoint='"';
$newText='phpfile.php';
$newFile = fopen($argv[1], "w");
fwrite(
$newFile,
preg_replace(
'#('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')#si',
'$1'.$newText.'$3',
$file
)
);
fclose($newFile);
?>
..and this HTML file:
<html>
<form method="POST" action="https://www.example.com/">
<input type="text" name="email">
<input type="password" name="password">
<input type="submit" name="button">
</form>
</html>
this does replace example.com with phpfile.php, but removes other lines of the HTML. This is what I'm left with after running the PHP script:
<html>
<form method="POST" action="phpfile.php">
</form>
</html>
I haven't been programming in PHP for long and some help would be appreciated.
First of all
Bad idea on replacing strings in files. A good way to approach this is having a php file with a variable which you could programatically define as the action file to be posted at.
Anyway
As that was not the question, I don't know what those # were supposed to mean, but in PCRE(php regex parser), here is the fixed code :
preg_replace(
'/('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')/i',
'$1'.$newText.'$3',
$file
)
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$fn=$_POST["filename"];
$content=$_POST["txt"];
$fp= fopen("abc.txt", "w") or die("unable to open file");
fclose($fp);
}
?>
<html>
<head>
<title>Files</title>
</head>
<body>
<center>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post" id="show">
<input type="text" name="filename" placeholder="File Name"><br>
<textarea name="txt" id="txtarea" id="txtarea" rows="20" cols="50" placeholder="Text goes here..."></textarea><br>
<input id="ixtbtn" type="submit" id="tabuttton" value="Done">
</form>
</center>
</body>
</html>
Php is automatically getting called on page load. But I need it to be called when submit button is pressed.
<?php
if (isset($_POST['submitted']))
{
$fn=$_POST["filename"];
$content=$_POST["txt"];
$fp= fopen("abc.txt", "w") or die("unable to open file");
fclose($fp);
}
?>
<html>
<head>
<title>Files</title>
</head>
<body>
<center>
<?php echo "<form action='" . htmlspecialchars(#_SERVER['PHP_SELF']) . "' method='post' id='show'>"; ?>
<input type="text" name="filename" placeholder="File Name"><br>
<textarea name="txt" id="txtarea" id="txtarea" rows="20" cols="50" placeholder="Text goes here..."></textarea><br>
<input id="ixtbtn" type="submit" id="tabuttton" value="Done" name='submitted'>
</form>
</center>
</body>
</html>
Played with your code a bit and noticed two things: (1) your statement is outside your php code block and in straight html. (2) On my system (Firefox or Safari on OSX) that meant that the php $_SERVER['PHP_SELF'] was never translated back to php, it was treated as text and the post command kept posting to the server looking for a URL starting with the php If I use echo inside php to output your HTML, the variables ($_SERVER) are not substituted and go in as straight text. So I put your html into a string variable first to translate the variables and then echoed your html. Saved the file as an PHP file instead of as an html file and your code worked just fine. So, four net changes.
Move the closing php statement (?>) to the end of the file
copy the HTML into a php string
$outputString = "";
$outputString = $outputString.'<!DOCTYPE html>';
$outputString = $outputString.'<html><head><meta charset="utf-8"><title>Files</title>';
$outputString = $outputString.'</head><body><center>';
$outputString = $outputString.'<form action="'.$_SERVER['PHP_SELF'].'" method="post" id="show">';
$outputString = $outputString.'<input type="text" name="filename" placeholder="File Name"><br>';
$outputString = $outputString.'<textarea name="txt" id="txtarea" id="txtarea" rows="20" cols="50" placeholder="Text goes here..."></textarea><br>';
$outputString = $outputString.'<input id="ixtbtn" type="submit" id="tabuttton" name="myButton" value="Done">';
$outputString = $outputString.'</form></center></body></html>';
Echo the string
Save as .php instead of .html