Form action GET input | the submit button sends the action + route? id="what the user typed" '

1

I researched a lot on how to do this and could not answer. Here's the situation: I have a listById method that needs to get the id that comes in the URL through the form. It turns out that when I submit the form, it forwards to the URL that is in the action +? Id = # o_que_o_usuario_digitado #. Hence my route manager can not handle this.

HTML:

<form class="container" action="/listById/" method="GET">
        <input class="form-control" type="text" name="id" id="id">
        <button id="btn-search" class="btn btn-primary" type="submit">Search</button>
    </form>

Route Manager:

app.get('/listById/:id', controlcontact.listById);

METHOD called in route:

contact.listById = (req,res)=>{

request.get(externalApi+req.params.id,
    (error, response, body) => {
        res.render('listById',{
            contact: JSON.parse(body)
        })
    });  
}

Example: if the input is typed "12345", the submit forwards to "/ listById /? id = 12345" I wanted to resolve this just by using HTML, without having to create javascript function.

    
asked by anonymous 24.11.2018 / 22:58

1 answer

0

That's right, what the user will submit will be submitted as parameters in the URI format.

On the server side you need to parse these parameters using url.parse(req.url, true) . If you use express, this is already done automatically by a middleware for you, instead of using req.params.id , you should access that value in req.query.id .

In addition, there is no need to declare the id in your route manager, just app.get('/listById', controlcontact.listById);

    
25.11.2018 / 00:13