2012-06-22 12 views
1

단추를 클릭하면 텍스트 상자의 텍스트를 .txt 파일에 쓰고 다운로드해야합니다. 어떻게 든 isset 함수가 작동하지 않습니다, 나는 이미 <form와 PHP 파일을 연결하려고 시도했지만 텍스트 텍스트를 읽을 수 없습니다. 당신은 HTML의 폼 태그를 추가하지 않은텍스트 상자 텍스트 가져 오기

<?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> 

답변

3

는 첫째로, 당신은 당신의 데이터를 제출할 수하는 <form> 태그를해야합니다 당신은 또한 당신의 <button type="submit" 현재 HTML에서 <input type="submit"

+0

+1 그게 전부 야! :) – Havelock

+0

감사합니다! 이미

태그를 시도했지만 메서드 게시가 없으면 작동했습니다! – xoxox

0

:

여기 내 코드입니다.

0

으로해야 할 수도 있습니다

<body> 
<form action="add your php filename here" method="post"> 

... 

</form> 
</body> 

을, 당신은 필요 <form> 태그

here.

+2

PHP가 자체 파일로 다시 게시되는 경우 양식과 동일한 파일에 PHP를 넣을 수없는 이유는 무엇입니까? – andrewsi

+0

전적으로 틀릴 수도 있습니다. 링크를 제공 할 수 있습니까? – SomeKittens

+1

당신은 어떤 파일을 동작으로 지정할 수 있습니다 - 실제로 동일한 파일을 사용하도록 기본 설정되어 있습니다. 문제가 발생하는 유일한 시간은 양식이 일반 HTML 파일 인 경우입니다. – andrewsi

1

폼 태그를 추가하고 PHP 문서 같은 파일 내에있는 경우 다시 같은 페이지에 게시에 대해 읽어보십시오.

<?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> 
관련 문제