javascript 팝업 창에서 입력받은 값 post 방식으로 처리하기
조회수 2755회
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 답변
-
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"); }); }); }
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력