Solution 1 :

if you are using the GET method to submit the form then use req.query.titlesearch :

router.get('/search', (req, res) => {
    console.log(req.query);
})

if you are using the POST method to submit the form then req.body.titlesearch

router.post('/search', (req, res) => {
    console.log(req.body);
})

Problem :

I have this function to create a search bar using the Express Framework in Node.JS, Handlebars and MySQL. The connection to MySQL is already functional, body parser already installed, and i already checked the query hardcoded straight inside phpmyadmin (without the req.body) and it worked, but it doesn’t work when i applied it to Node.JS

inside the handlebars :

<form role="form" action="naskah_form/search" method="get">
        <input type='text' name='titlesearch' id='q' placeholder='Enter a keyword...' required autocomplete='off'>
        <input type='submit' value='Search' id='submit'>
</form>

inside naskah_form.js :

router.get('/search', (req,res)=>{
    let sql = "SELECT * FROM upload_test where title LIKE '% "+ 
         req.body.titlesearch + "%'"
    conn.query(sql, (err,results)=>{
    if (err) throw err
        res.json(results[0])
    })
})

module.exports = router

I expect the result would be to at least show a list of json of the searched word let’s say ‘q’. The url is http://localhost:3000/naskah_form/search?titlesearch=q but inside it is blank.
Can anyone guide me on how to approach this issue?

Comments

Comment posted by WeerBeez

thank you for the answer but i just changed it into query and it still won’t show up

Comment posted by coderman401

will you please tell me what response you are getting in

Comment posted by localhost:3000/naskah_form/search?titlesearch=q

Let’s say i type the word ‘q’ , the result on the page is still white blank on both of the method but the url is

Comment posted by coderman401

try to debug your code try:

Comment posted by WeerBeez

I have finally solved the issue and use

By