Search⌘ K
AI Features

Using Request Methods

Explore how to use request methods in AdonisJs to capture values from submitted HTML forms and retrieve URL query parameters. This lesson guides you through working with CSRF protection, handling inputs with request.input() and request.only(), and managing data from both forms and URLs effectively in your controllers and routes.

Getting values from a submitted form

Let’s say we want to get the values from a submitted form. In the code below, we will create an HTML form to get the product ID and product name from the user. We will then get these values inside a controller.

'use strict'

class TestController {
  //Extracting the view class of the HTTP context
  add({ view }) {
    return view.render('add')
  }
  product({ request }) {
    const product_id = request.input('product_id')
    const product_name = request.input('product_name')
    return product_id + ', ' + product_name
    // const form = request.only(['product_id', 'product_name'])
    // return form.product_id + ', ' + form.product_name
  }
}

module.exports = TestController

Press Run and wait for the output to be displayed in the Output tab. You will see a form. Then, click on the link under the Run button, ...