0

I am going to get input from a message form (HTML) in js, but it shows The method is not allowed for the requested URL. Can anyone help me find where is the issue? Thanks!

js code:

var socket = io.connect("http://" + document.domain + ":" + location.port);
socket.on("connect", async function () {
  var usr_name = await load_name();
  if (usr_name != "") {
    socket.emit("event", {
      message: usr_name + " just connected to the server!",
      connect: true,
    });
  }
  var form = $("form#msgForm").on("submit", async function (e) {
    e.preventDefault();

    // get input from message box
    let msg_input = document.getElementById("msg");
    let user_input = msg_input.value;
    let user_name = await load_name();

    // clear msg box value
    msg_input.value = "";

    // send message to other users
    socket.emit("event", {
      message: user_input,
      name: user_name,
    });
  });
});

HTML form:

<form id="msgForm" action="" method="POST" style="bottom:0; margin: 0% 0% 0% 0%;">
    <div class="input-group mb-3">
          <input type="text" class="form-control" placeholder="Message" aria-label="Message" id="msg">
          <div class="input-group-append">
            <button class="btn btn-success" type="submit" id="sendBtn">Send</button>
          </div>
    </div>
</form>
Jamie Liu
  • 11
  • 1

1 Answers1

0

Well, this is quite simple and is basic JavaScript.

  1. Fetch the input using querySelector or get it by its ID.
  2. Take the input and use .value
const input = document.querySelector("#msgForm");
console.log(input.value);