Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

html - Can't save textarea to file using php?

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...