xoxox
xoxox

Reputation: 808

Get textbox text

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>

Upvotes: 1

Views: 1712

Answers (4)

Shehzad Bilal
Shehzad Bilal

Reputation: 2523

You haven't added the form TaG in HTML.

Upvotes: 0

Robert
Robert

Reputation: 2282

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>

Upvotes: 1

SomeKittens
SomeKittens

Reputation: 39522

In your current HTML, you need a <form> tag.

Try reading about this here.

Upvotes: 0

andrewsi
andrewsi

Reputation: 10732

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"

Upvotes: 3

Related Questions