본문 바로가기

웹/JSP

[JSP] <ajax> 비동기 아이디 중복 검사

HTML

<div class="col-lg-8">
	<input class="form-control p-3  border-secondary" 
    	type="text" name="MID" id="MID" placeholder="아이디">	
</div>	
<div class="col-lg-4">
	<button class="btn border border-secondary 
    	text-primary rounded-pill px-4 py-3" id="checkIdDupl" 
    	type='button' onclick="checkMID()">중복 검사</button>	
</div>

 

ajax

<script type="text/javascript">
	var MIDResult;
		function checkMID() {
			// 사용자가 입력한 아이디 가져오기
			var MID = $("#MID").val();
			if(MID === ""){
				Swal.fire({
					icon: 'error',
					title: '아이디 검사',
					text: '아이디를 입력해주세요.',
				})
				return 0;
			}
			// AJAX 요청 보내기
			$.ajax({
				type: "POST", // 또는 "GET"
				url: "CheckId", // 서버에서 아이디 중복 확인을 처리할 PHP 파일 경로
				data: { 'MID': MID },
				success: function(data) {
					MIDResult = data
					if(data === "suc"){
						Swal.fire({
							icon: 'success',
							title: '아이디 검사',
							text: '사용 가능한 아이디 입니다.',	
						})
					} else {
						Swal.fire({
							icon: 'error',
							title: '아이디 검사',
							text: '사용 불가능한 아이디 입니다.',
						})
					}
				}
			});
		}	
</script>

 

Servlet

@WebServlet("/CheckId")
public class CheckId extends HttpServlet {
	private static final long serialVersionUID = 1L;
    	public CheckId() {
        	super();
    	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    	throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
    	throws ServletException, IOException {
		String MID = request.getParameter("MID");
		MemberDTO mDTO = new MemberDTO();
		MemberDAO mDAO = new MemberDAO();
		mDTO.setSearchCondition("아이디중복검사");
		mDTO.setMid(MID);
		mDTO=mDAO.selectOne(mDTO);
		PrintWriter out = response.getWriter();
		if(mDTO==null) {
			out.print("suc");
		}else {
			out.print("fail");
		}
	}
}

' > JSP' 카테고리의 다른 글

[JSP] <plugin> starability  (0) 2024.01.17
[JSP] <jquery> 비밀번호 입력 확인  (0) 2024.01.16
[JSP] <JSP> 세션 값 저장  (0) 2024.01.16
[JSP] <JavaScript> 비동기 기초  (0) 2024.01.15
[JSP] <EL> 기초  (0) 2024.01.11