Solution 1 :

You didn’t specify, but it appears you’re using express.

Use the req.query object rather than req.params, as params are used for URLs with routes, not query parameters. With that being said, you do not need the :search in your request URL match. I can’t explain why it’s receiving a value of ‘h’, but this should solve your problem.

Solution 2 :

you need to use POST instead of GET in the form Method

<HTML>
    <form action="/request/search" method="POST">
        <input
          type="text"
          name="search"
          id="search"
          placeholder="Search for Books"
          class="search"
        />
        <button class="search-btn" type="submit">Submit</button>
  </form>

IN js side.
app.get('/request/search', (req, res) => { console.log(req.body); res.redirect('/'); });

Problem :

I am trying make a static search bar that currently is redirected to ‘/’. That is just the current situation. But the main problem is that whatever the input is, the output is always ‘h

I am using nodejs and ejs

.

request.ejs

      <form action="/request/search" method="GET">
            <input
              type="text"
              name="search"
              id="search"
              placeholder="Search for Books"
              class="search"
            />
            <button class="search-btn" type="submit">Submit</button>
        <div class="imae"></div>
      </form>

server.js


app.get('/request/search?:search', (req, res) => {
  console.log(req.params);
  res.redirect('/');
});

output

{ search: 'h' }

Am I Doing something wrong, Please Help

By

Leave a Reply

Your email address will not be published. Required fields are marked *