메뉴 건너뛰기

프로그램언어

조회 수 8456 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

form tag를 사용해서 submit 버튼을 누를시 comment_ok.php로 form 내용들을 전달하고 insert 한다. insert가 성공할 시 해당 글에 대한 모든 댓글을 출력하는 getAllList(); 를 호출한다.


호출 받은 getAllList()는 comment_list.php?board_num='게시물 번호' 으로 요청하고 요청받은 comment_list.php는 select 쿼리를 실행하고 배열 형태로 댓글 데이터를 담고 그 배열을 json 형태고 변환 시킨다음 반환한다.


화면.php에 있는 getJSON을 통해서 json 데이터를 받고 div에 넣는다. 

게시글에 들어오자마자 댓글을 보게 할려면 

	$(document).ready(function(){
		getAllList();
	});

를 통해 바로 출력시킨다.



화면.php

<script>
	$(document).ready(function(){
		getAllList();
	});

	var str = "";

	function getAllList(){
		var board_num = $("#board_num").val();

		console.log("getAllList()");
		console.log("board_num" + board_num);

		$.getJSON("comment_list.php?board_num="+board_num, function(data){
			console.log(data);

			$(data).each(function(){
				console.log(data);

				str += "writer : "+this.writer+"<br> title : " + 
					this.comment_title + "<br> content : " + 
					this.comment_content + "<br>";
			});


			$("#replies").html(str);
		});
	}

	$(document).on("click", "#comment_btn", function() {
		alert("click");

		var formData = $("#comment_form").serialize();

		$.ajax({
			type : 'POST',
			url : 'comment_ok.php',
			data : formData,
			success : function(response){
				if(response == 'success'){
					alert("success");
					getAllList();
				}
			}
		});
    });
</script>


등록처리하는.php (comment_ok.php)

<?
	include 'db_connect.php';
	include 'session.php';

	$board_num = $_POST['board_num'];
	$writer = $_POST['writer'];
	$comment_title = $_POST['comment_title'];
	$comment_content = $_POST['comment_content'];

	$sql = "insert into comment (board_num, writer, comment_title, comment_content, reg_date) 
			values ('$board_num', '$writer', '$comment_title', '$comment_content', now())";
	$result = mysql_query($sql) or die("Error :	" . mysql_error());

	if($result){
		echo "success";
	}
?>



댓글들 불러오는.php (comment_list.php)

<? include 'db_connect.php'; include 'session.php'; $board_num = $_GET['board_num']; $sql = "select * from comment where board_num = '$board_num' order by reg_date desc"; $result = mysql_query($sql) or die("Error : " . mysql_error()); $resultArray = array(); while($row = mysql_fetch_array($result)){ array_push($resultArray, array('comment_idx' => $row[0], 'writer' => $row[2], 'comment_title' => $row[3], 'comment_content' => $row[4])); } echo json_encode($resultArray); ?>




List of Articles
번호 제목 날짜 조회 수
200 공백문자 체크 2019.01.08 1625
199 경로 제외한 파일 이름만 선택하는 방법, Basename() 2020.11.23 429
198 게시판 페이징 기법과 개념 file 2017.03.06 17323
197 게시판 내용 숨김 클릭시 내용 출력 [ 참고 ] 2018.07.24 4767
196 검색어 처리 루틴 2015.04.14 20778
195 간단한 캡차파일 만들기 captcha file 2023.01.12 261
194 간단한 PHP 파일 업로드, 다운로드 구현 2017.03.06 28547
193 가변변수로 만든 배열 2021.03.26 272
192 [이클립스]PHP 개발환경 만들기 file 2018.07.04 7941
191 [PHP기초] 함수와 객체의 비교 2021.03.27 291
190 [PHP기초] 접근제어자(access modifier) 2021.03.27 283
189 [PHP기초] 생성자(인스턴스 초기화) 2021.03.27 265
188 [PHP기초] 상속기본 2021.03.27 214
187 [PHP기초] 데이터 집합 - 배열다루기 2021.03.27 263
186 [PHP] 한글명 파일 다운로드받기 2014.03.26 20551
185 [PHP] 서버 운영하시는분 서버 꺼졌을때 메시지 띄우기 2015.04.06 20324
184 [PHP] 게시판 글쓰기와 이미지 파일 DB 저장 및 불러오기 예제 2017.02.19 35886
183 [PHP] POST 로 넘어온 모든 변수값 확인하기 2015.03.25 23337
182 [PHP 기초] 함수에 관해서 2021.03.27 268
181 www가 붙은 도메인과 안붙은 같은 도메인, 로그인 세션 유지 2017.03.07 17080
Board Pagination Prev 1 ... 3 4 5 6 7 8 9 10 11 12 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved