메뉴 건너뛰기

프로그램언어

조회 수 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
번호 제목 날짜 조회 수
280 DB 내용을 화면에 출력(이중 for 문) 2015.04.14 21246
279 POST값 통째로 인코딩하기 1 2015.04.06 21175
278 체크박스, post 로 넘기고 받아서 다시 체크하기, checkbox 2017.03.07 21131
277 PHP continue 문 file 2015.04.14 21097
276 FPDF - PHP로 PDF 만들기 2014.02.27 21075
275 RSSReader Class 제작 및 Reader 만들기 file 2016.08.22 21042
274 Ajax로 구연한 실시간 서버시간출력 file 2017.03.06 21031
273 PHP switch 문 file 2015.04.14 20993
272 PHP error 메시지 출력 file 2015.04.14 20924
271 preg_match (정규표현식 매치를 수행합니다) 2016.12.23 20843
270 검색어 처리 루틴 2015.04.14 20778
269 array_slice 배열의 일부를 추출 2016.12.23 20775
268 템플릿 관련 정보 2016.08.22 20732
267 PHP 소스 : 이미지 리사이즈, 섬네일 2014.03.26 20704
266 HTTP Protocol의 data method - GET / POST 2016.04.22 20637
265 [PHP] 한글명 파일 다운로드받기 2014.03.26 20551
264 문자열 치환하기 2015.04.14 20492
263 stripslashes — 따옴표 처리한 문자열을 풉니다 2016.12.23 20486
262 quotemeta 모든 메타 문자앞에 역슬래쉬를 붙인 문자열을 반환 2016.12.23 20461
261 stripcslashes — addcslashes()로 인용한 문자열을 되돌림 2016.12.23 20442
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved