[JS + jquery] Ajax๋กœ ๊ฐ„๋‹จํ•œ ๋กœ๊ทธ์ธ ๋งŒ๋“ค๊ธฐ

2021. 11. 2. 13:49ใ†HTML + CSS + JS/JS ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

728x90

๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด 

 

1. php 

2. html 

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title> Ajax </title>
<link rel="stylesheet" href="css/login.css" />
<script src="js/jquery.js"></script>
<script>
$(function() {
  var $frm = $(".login_f");
  $frm.on("submit", function(e) {
    e.preventDefault();
    var myData = $frm.serialize();

    $.ajax({
      type: "POST",
      url: $frm.attr("action"),
      data: myData,
      success:function(res) {
        if(res) {
         var jsonData = JSON.parse(res);
         var message = jsonData.user_name + 
         "( " + jsonData.user_id + " )" 
         + "๋‹˜ ๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค";
         $(".login_wrap").html(message);
        }
      }
    });
  });
});
</script>
</head>
<body>
<div class="login_wrap">
  <h1>๋กœ๊ทธ์ธ</h1>
  <form class="login_f" method='post' action='member_login_ok.php'>
    <p>
    <label for="user_id">์•„์ด๋””</label>
    <input type="text" name="user_id" id="user_id" value="korean" />
    </p>
    <p>
    <label for="user_pw">๋น„๋ฐ€๋ฒˆํ˜ธ</label>
    <input type="password" name="user_pw" id="user_pw" value="12345" />
    </p>
    <p><input type="submit" value="๋กœ๊ทธ์ธ" class="login_btn" /></p>
  </form>
</div>
</body>
</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90