Process the value entered in the javascript pop-up window in a post manner

Asked 2 years ago, Updated 2 years ago, 49 views

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">Enter visitor name</a>
    </form>
  </body>

app.js

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

    });
});

The server environment is node.js.

I'd like to implement it as above and save the value entered in the pop-up window in mysql by post method.

I don't know how to receive the value in the pop-up window value -> post method...

I tried to access console.log(req.body.editname); from app.js...

Please answer... ㅠ<

sweetalert2 node.js

2022-09-22 21:37

1 Answers

jquery's ajax makes it easy to transfer values.

Don't wrap up the "Visitor name" part with a form If a pop-up appears, try sending it to ajax in the incoming callback function.

 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;
                }

                // You can send it using the jquery ajax method below. 
                $.post("/edit_username", { username: inputValue} ).done(function() { 
                swal("Nice!", "You wrote: " + inputValue, "success"); });
            });


      }


2022-09-22 21:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.