Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...