메뉴 건너뛰기

프로그램언어

조회 수 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 몫과 나머지 구하기 2017.03.07 18471
199 PHP에서 데이터를 엑셀(Excel)로 저장 2017.02.19 18428
198 정규표현식 검사 도구 (ereg, eregi) 2016.12.23 18395
197 이스케이프 함수 (htmlentities) 2016.12.23 18375
196 PHP 속도 테스트 20 가지 2017.03.06 18306
195 PHP EXCEL export시 시트 이름 지정하여 여러 시트에 데이터 쓰기 2017.03.06 18266
194 fileatime 파일에 최종적으로 접근한 시간을 반환 2016.12.23 18117
193 문자열의 태그를 그대로 출력 (htmlspecialchars) 2016.12.23 18069
192 is_array — 변수가 배열인지 확인 2016.12.23 18048
191 gd_info 사용 가능한 GD 라이브러리에 대한 정보를 배열로 반환 2016.12.23 17989
190 네이버 자동 띄어쓰기를 이용하기 2017.03.27 17940
189 PHP로 엑셀 자료 MySQL에 넣기 2017.03.06 17875
188 어떤 스마트폰으로 접속했는지 알아내는 소스 , (안드로이드 아이폰 ) 2014.03.26 17716
187 PHP 날짜 함수 2017.04.13 17673
186 http://홈주소/?mode=xxx 하는방법 2017.03.07 17642
185 그누보드, 여분필드 사용팁 2017.03.06 17606
184 정규식 2017.04.13 17599
183 PHP 도메인 이름이나 웹문서 주소 알아내기 2017.03.06 17589
182 text파일에 한줄씩 내용추가하기 2017.03.06 17537
181 문자열에서 태그를 제거 (strip_tags) 2016.12.23 17522
Board Pagination Prev 1 ... 3 4 5 6 7 8 9 10 11 12 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved