mardi 7 janvier 2020

How to get the value for the search function in Node Js?

so i have this function to create a search bar using Express Framework In Node Js, Handlebars, and MySQL. All the connection to the mysql is already connected, body parser already installed, and i already checked the query hardcoded straight inside phpmyadmin (without the req.body) and it worked but it doesnt work when i applied it to node js.

inside the handlebars :

        <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 atleast show a list of json of the searched word. Any solutions?




Aucun commentaire:

Enregistrer un commentaire