zb22
zb22

Reputation: 3231

How to get data from a form dropdown select element?

I'm using Express.js and trying to get form data of a dropdown select element, I tried with body-parser but what I get with eq.body.method_select is undefined. I didn't find much info about how to do this on the web.

Here is my code html code:

  <form action="url_analyse">
   <div class="col-lg-6">

       <div class="input-group">

           <select class="custom-select mb-2 mr-sm-2 mb-sm-0" name="method_select" id="inlineFormCustomSelect">
               <option value="5">Regular Search (Short)</option>
               <option value="10">Intense Search (Long)</option>
               <option value="20">Deep Search (Very Long)</option>
           </select>

           <input type="text" name="url_input" class="form-control" placeholder="Enter URL">

          <span class="input-group-btn">
                <button class="btn btn-secondary">Go!</button>
          </span>

       </div>


   </div>

Here is my js code:

app.get('/url_analyse', function(req, res) {
   console.log(req.body.method_select);
})

Hope you can help me with that. Thank you.

Upvotes: 0

Views: 1659

Answers (1)

mscdex
mscdex

Reputation: 106696

There are two issues here:

  • You might want to explicitly add the leading forward slash: action="/url_analyse"
  • The default form submission HTTP method is GET, which means that form fields will be passed in the query string portion of the URL. This means you will instead need to access req.query to get at the form fields. You only need body-parser and req.body when you use POST and other methods (with the appropriate enctype) to submit your form.

Upvotes: 2

Related Questions