본문 바로가기
Language/JavaScript

JSP 화면에서 본인글만 삭제 가능하게

by 하늘둥둥 2019. 2. 20.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>게시글 보기</title>

</head>

<body>

<style type="text/css">

table.table {

    border-collapse: separate;

    border-spacing: 1px;

    line-height: 1.5;

    margin: 20px 10px;

}

table.table th {

    width: 155px;

    padding: 10px;

    font-weight: bold;

    color: #fff;

    background: #ce4869 ;

}

table.table td {

    padding: 10px;

    border-bottom: 1px solid #ccc;

    background: #eee;

}

</style>

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

<script type="text/javascript">

$(function(){

var able = $('#userId').val(); // 로그인된 유저의 'id'값

var id = $('#id').val(); // 게시글의 'id'값

if(able==id){

$("#submit").removeAttr("disabled"); // 버튼을 활성화 시킨다.

$('#delBtn').click(function(){

var pw = $('#password').val(); // 게시글의 'passwowrd'값

var idx = $('#idx').val(); // 게시글의 'idx'값

alert(pw);

var ck = prompt("본인 비밀번호를 입력해 주세요"); // prompt를 여러번 이용하기 위해 변수에 넣어준다

if(ck == null){

alert('취소되었습니다.');

}else if(ck == ""){

alert('아무것도 입력되지 않았습니다.');

}else {

if(ck == pw){

alert("삭제되었습니다.")

viewForm.action="delete"; // 조건을 만족하면 폼안의 값들을 컨트롤러의 '/delete'로 넘겨줄 경로

viewForm.submit(); // 넘겨주는 명령

}else{

alert("비밀번호가 틀립니다.")

}

}

});

}else{

$("#delBtn").attr("disabled", "disabled"); // 버튼 비활성화

}

});


</script>

<form name="viewForm" method="post"> <!-- 폼으로 감싸줘야 데이터들을 넘겨줄 수 있다. -->

<input type="hidden" name="userId" id="userId" value="<%=session.getAttribute("id") %>" />

<input type="hidden" name="userpassword" id="userpassword" value="<%=session.getAttribute("password") %>" />

<input type="hidden" name="password" id="password" value="${vo.password}" />

<input type="hidden" name="id" id="id" value="${vo.id}" />

<input type="hidden" name="idx" id="idx" value="${vo.idx}" />

<table class="table" width="500" align="center">

<tr><th colspan="4">게시글 보기</th></tr>

<tr>

<td align="center" width="80">글번호</td>

<td align="center" width="90">작성자</td>

<td align="center" width="150">작성일</td>

<td align="center" width="80">조회수</td>

</tr>

<tr>

<td align="center">${vo.idx}</td>

<td align="center">

${vo.name}

</td>

<td align="center">

<fmt:formatDate value="${vo.writeDate}" pattern="yyyy.MM.dd(E) HH:mm"/>

</td>

<td align="center">${vo.hit}</td>

</tr>

<tr>

<td align="center">제목</td>

<td colspan="3">

${vo.subject}

</td>

</tr>

<tr>

<td align="center">내용</td>

<td colspan="3">

  ${vo.content}

  </td>

</tr>

<tr>

<td colspan="4" align="center">

<input type="button" value="수정하기" onclick="location.href='update?idx=${vo.idx}&currentPage=${currentPage}'"/>

<button type="button" name="delBtn" id="delBtn">삭제하기</button>

<input type="button" value="답변하기" onclick="location.href='reply?idx=${vo.idx}&currentPage=${currentPage}'"/>

<input type="button" value="목록보기" onclick="location.href='list?currentPage=${currentPage}'"/>

</td>

</tr>


</table>

</form>


</body>

</html>










'Language > JavaScript' 카테고리의 다른 글

비밀번호 재검사  (0) 2019.02.18

댓글