javascript 팝업 창에서 입력받은 값 post 방식으로 처리하기

조회수 2811회

test.ejs

<script type="text/javascript">
      function fn_edit_username(method, path) {

        method = method || "post";
        var form = document.createElement("form");
        form.setAttribute("method", method);
        form.setAttribute("action", path);

        swal({   title: "An input!",
                  text: "Write something interesting:",
                  type: "input",
                  showCancelButton: true,
                  closeOnConfirm: false,
                  animation: "slide-from-top",
                  inputPlaceholder: "Write something"
          }, function(inputValue) {
                if (inputValue === false)
                  return false;

                if (inputValue === "") {
                  swal.showInputError("You need to write something!");
                  return false;
                }

                swal("Nice!", "You wrote: " + inputValue, "success");
                document.body.appendChild(form);
                form.submit();

            });


      }
    </script>

  </head>
  <body>
    <form action="/edit_username" method="post" >
      <a href="javascript:fn_edit_username('post', '/edit_username')" data-user-id ="1">방문자명 입력</a>
    </form>
  </body>

app.js

app.post('/edit_username', function(req, res) {
  pool.getConnection(function(err, conn) {
      console.log(req.body.editname);

    });
});

서버 환경은 node.js입니다.

위처럼 구현하여 팝업창에서 입력받은 값을 post 방식으로 mysql에 저장하려고 하는데요.

팝업창 값 -> post 방식으로 값을 받는 방법을 모르겠네요...

app.js 에서 console.log(req.body.editname); 로 접근하려 했는데...

답변 부탁드립니다... ㅠㅠ

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기

    jquery의 ajax를 이용하면 쉽게 값을 전송할 수 있습니다.

    '방문자명 입력' 이 부분을 form으로 감싸지 마시고 팝업이 떴을경우 값이 들어오는 콜백함수 안에서 ajax로 전송해보세요.

     function fn_edit_username(method, path) {
    
            method = method || "post";
            var form = document.createElement("form");
            form.setAttribute("method", method);
            form.setAttribute("action", path);
    
            swal({   title: "An input!",
                      text: "Write something interesting:",
                      type: "input",
                      showCancelButton: true,
                      closeOnConfirm: false,
                      animation: "slide-from-top",
                      inputPlaceholder: "Write something"
              }, function(inputValue) {
                    if (inputValue === false)
                      return false;
    
                    if (inputValue === "") {
                      swal.showInputError("You need to write something!");
                      return false;
                    }
    
                    // 아래 부분 jquery ajax 메소드를 이용해서 전송하면 됩니다. 
                    $.post("/edit_username", { username: inputValue} ).done(function() { 
                    swal("Nice!", "You wrote: " + inputValue, "success"); });
                });
    
    
          }
    
    • (•́ ✖ •̀)
      알 수 없는 사용자
    • 감사합니다. 글 쓴지 오래 됐는데 이제라도 해답을 알게되어 다행입니다. ㅠ 상남자 2016.6.9 16:34

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)