메뉴 건너뛰기

프로그램언어

조회 수 8452 추천 수 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
번호 제목 날짜 조회 수
160 PHP 파일크기 단위 붙이기 (용량 변환) file size conversion source code 2018.07.04 5789
» AJAX를 활용하여 JSON 댓글 처리하기 (PHP) 2018.07.04 8452
158 날짜, 시간 포맷하기 (PHP) 2018.07.04 5230
157 쿠폰번호 발행 업데이트판. (간단한 클래스화[PHP4 기준] 등...) 2018.07.19 6018
156 fcm 푸시 알림 php 테스트 2018.07.19 6042
155 PHP 특정 디렉토리에 있는 파일 갯수 구하기 2018.07.19 5443
154 gcm 푸시 알림 php 테스트 2018.07.19 5582
153 PHP 소스코드 인코딩(암호화)하기 2018.07.19 6643
152 마우스 오버시 사진변환, 파일에러시 대체이미지 적용(소스일부) 2018.07.24 4584
151 게시판 내용 숨김 클릭시 내용 출력 [ 참고 ] 2018.07.24 4767
150 날짜계산 몇일까지.. [ ex)4 일전 new 표시 ] 2018.07.24 4523
149 MySQL테이블의 내용을 엑셀파일(xls)로 다운로드 하기 2018.07.24 4798
148 키를 이용한 암호화/복호화 함수입니다. 2018.07.24 5741
147 헤더이용 다운로드 받을시 바로열기부분 소스 2018.07.24 7320
146 웹서버조회 소스 2018.07.24 4543
145 날짜/시간함수 정리 2018.08.29 2429
144 한글이 깨져서 나올 때 - iconv 2018.08.29 3933
143 PHP에서 모든 세션 정보를 화면에 출력하는 방법 2018.08.29 2693
142 PHP에서 자료, 데이터의 타입을 확인하는 방법, gettype() 2018.08.29 2446
141 PHP에서의 대칭 암호화/복호화 ― 간단한 예제에서 DB 입/출력까지 2018.09.14 3548
Board Pagination Prev 1 ... 5 6 7 8 9 10 11 12 13 14 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved