-1

I have a html with an user input and i want that input to be transfered to the nodejs document.

html code:

<script type="text/javascript">
              function redirect() {
                const name = document.getElementById("field").value;
              }
            </script>
<form action="stats.html">
                <input id="field" type="text" name="name" placeholder="Player Name...">
                <input id="button" type="submit" value="SEARCH" onclick="redirect()">
            </form>

and the documents are located

index.js

public/index.html

  • this may help you https://stackoverflow.com/questions/15568851/node-js-how-to-send-data-from-html-to-express?answertab=active#tab-top – jsduniya Mar 22 '19 at 07:52

2 Answers2

1

you have to define a route with in your node server

app.post('/mydata',function(req,res) {
  let data = req.body.name;
   console.log(name);
});`

and send ajax request to this route from your redirect() method:

function redirect() {       
   const name = document.getElementById("field").value;       
  $.ajax({
    type: "POST",
    url: '/mydata',
    data: {name:JSON.stringify(name)},
    success: success
});
Fahad Mahmood
  • 159
  • 1
  • 10
1

Try this

 <script type="text/javascript">
              var field;
              function onload(){
               field = document.getElementById("field")
         }

       function redirect() {
                var name = field.value;
              }
            </script>
<form action="stats.html" onload="onload()">
                <input id="field" type="text" name="name" placeholder="Player Name...">
                <input id="button" type="submit" value="SEARCH" onclick="redirect()">
            </form>