특정 필드를 txt로 저장하고 싶습니다.

특정 필드를 txt로 저장하고 싶습니다.

QA

특정 필드를 txt로 저장하고 싶습니다.

본문

<input type="button" value="저장하기">

 

저장하기 버튼을 누르면 게시판의 wr_1 번 필드를 txt 파일로 컴퓨터에 저장되게 하고싶은데 어떻게 하면 될까요?

이 질문에 댓글 쓰기 :

답변 3

PHP로 처리할 필요없이, 자바스크립트로만 처리 가능합니다.

hidden으로 하든, 스크립트에 값을 바로 대입하든 자유롭게….


<input type="text" id="wr_1" value="테스트"><!-- value="<?=$view['wr_1']?>" -->
<button type="button" onclick="save()">저장하기</button>
<script>
function save() {
  var obj = document.createElement('a');
  obj.setAttribute('href', 'data:text/plain;charset=utf-8,'+encodeURIComponent(document.querySelector('#wr_1').value));
  obj.setAttribute('download', 'wr_1.txt'); // DateTime 이용하면 좀 더 깔끔하겠죠?
  obj.style.display = 'none';
  document.body.appendChild(obj);
  obj.click();
  document.body.removeChild(obj);
}
</script>

form 처리 해주는 부분에서 파일 생성하시면 됩니다.


<?php
$myfile = fopen("file.txt", "a+") or die("Unable to open file!");
fwrite($myfile, $wr_1);
fclose($myfile);
?>

 

출처 https://phpheaven.tistory.com/32

답변을 작성하시기 전에 로그인 해주세요.
전체 74
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT