메뉴 건너뛰기

프로그램언어

조회 수 8454 추천 수 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 PHP와 HTML과 자바스크립트의 관계 2021.03.26 311
279 TIFF, GIF 여러장 변환 file 2021.03.26 323
278 정규표현식 2021.03.26 311
277 date() 함수의 출력 형식 2021.03.26 241
276 확장자 추출 하기 2021.03.26 308
275 서브도메인 세션 공유 2021.03.26 537
274 가변변수로 만든 배열 2021.03.26 272
273 HEREDOC <<< ( PHP에서 echo로 HTML쉽게 표시하기 ) 2021.03.26 371
272 배열 연산자 []= 에 대해서 2021.03.26 231
271 Predefined Variables (미리 정의된 변수들) 2021.03.26 274
270 current() next() key() 그외 배열관련 함수 2021.03.26 253
269 비교연산자 ( === 에 관해서 ) 2021.03.25 307
268 큰따옴표(") 와 작은따옴표(') 2021.03.25 270
267 mysql_free_result(); 관련 오류 2021.03.25 288
266 phpMyAdmin WebMysql 에 CSV 엑셀 파일 업로드 입력하기 ( Excel / Upload / data / 데이터 / 데이타 ) file 2021.03.25 754
265 PHP + 유튜브(youtube) 동영상 업로드 연동 소스 2021.01.21 1135
264 다중파일 업로드 + 이미지 미리보기 (Javascript, jQuery ) file 2020.12.15 912
263 경로 제외한 파일 이름만 선택하는 방법, Basename() 2020.11.23 429
262 쿠키 확인 후 만료시 세션 파괴하는 방법 2020.11.23 314
261 DAUM 지도 API 좌표→주소(주소->좌표) 변환 2020.10.05 431
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved