db연결 된<select>태그 value 값 변경

package co.jay.prj.command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import co.jay.prj.comm.Command;
import co.jay.prj.member.service.MemberService;
import co.jay.prj.member.service.MemberVO;
import co.jay.prj.member.serviceImpl.MemberServiceImpl;

public class AjaxAuthorUpdate_re implements Command {
	private String result;
	@Override
	public String run(HttpServletRequest request, HttpServletResponse response) {
		// 회원 권한 수정
		MemberService memberDao = new MemberServiceImpl();
		MemberVO vo = new MemberVO();
		
		vo.setId(request.getParameter("id"));
		vo.setAuthor(request.getParameter("author"));
		int n = memberDao.memberAuthorUpdate(vo);
		if(n != 0) {
			result = "yes";
		}else {
			result = "no";
		}
		return "ajax:"+result;
	}

}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="js/jquery-3.6.0.min.js"></script>
<script type="text/javascript">
	function authorEdit(id, status) {
		var Id = id;
		var Author = $("#author"+ status).val(); // status로 목록에서 클릭한 author값을 들고옴
	// ajax로 처리 
		$.ajax({
			url : 'ajaxAuthorUpdate.do',
			type : 'post',
			data : {
				id : Id,
				author : Author
			},
			dataType : 'text',
			success : function(data) {
				if (data == 'yes') {
					alert(Id + "님의 권한이 변경 되었습니다.")
				} else {
					alert("권한이 변경 실패했습니다.")
				}
			}
		});
	}
</script>
</head>
<body>
<%@ include file="../home/header.jsp" %>
	<div align="center">
		<div><h1>멤 버 목 록</h1></div>
		
		<div>
			<table border="1">
				<tr>
					<th width="150">아이디</th>
					<th width="150">이  름</th>
					<th width="150">전화번호</th>
					<th width="400">주  소</th>
					<th width="100">권  한</th>
					<th width="100">권한수정</th>
				</tr>
				<c:forEach items="${members }" var="member" varStatus="status">
				<tr onmouseover="this.style.background='#E0F8F7'"
					onmouseleave="this.style.background='#F7F8E0'">
					<td align="center">${member.id }</td>
					<td align="center">${member.name }</td>
					<td align="center">${member.tel }</td>
					<td>&nbsp;${member.address }</td>
					<td align="center">
						<select id="author${status.count }" name="author">
							<option value="ADMIN" <c:if test="${member.author eq 'ADMIN' }">selected</c:if>>ADMIN</option>
							<option value="USER" <c:if test="${member.author eq 'USER' }">selected</c:if>>USER</option>
						</select>
					</td>
					<td align="center"><button type="button" onclick="authorEdit('${member.id }', '${status.count }')">변경</button>
					</td>
				</tr>
				</c:forEach>
			</table>
		</div><br>
		<button type="button" onclick="location.href='home.do'">홈 으로</button>
	</div>
</body>
</html>

<c:forEach varStatus="status">을 통해 status.count 사용

좋은 웹페이지 즐겨찾기