Search mysql database with node in html

Search mysql database with node in html
0

#1

I connected a database to node and am trying to create an HTML page to search the database. I would rather not use EJS. I think I have to use a POST request in the HTML AJAX and connect it with a POST request in node.

Here is what I’m thinking:

app.post("/cities/:city", function(req, res) {
    db.hospitalinfo.findAll({
        where: { city: req.params.city }
    }).then(function (result) {
        res.json(result);
        console.log("res--->"+result);
        console.log("req--->"+req.params.city);
    });
});

Here’s the HTML:

<form id="author-form" method="POST">
      <select id ="dropDownId">
          <option value="Opp" >Opp</option>
          <option value="Boaz">Boaz</option>
      </select>
      <input class="SubmitButton" type="submit"  id="click" style="font-size:20px;" />
</form>

Now here’s where I’m stuck. I need to grab the value from the select statement:

var nameInput = $("#dropDownId :selected");

I don’t know how to actually send nameInput to the URL so my post statement will work. I probably don’t completely understand how these routes work. This is my first project by myself. I would like to grab the nameInput, send it to the server via AJAX, and search my database with it. Right now it’s returning an empty object. Thank you for your help.


#2

The param :city in your route is basically everything you put in there:

"/cities/1"; // 1
"/cities/berlin";  //berlin
"/cities/adsfaf"; //adsfaf

Btw the point of query params is to use them in a GET request ( so app.post would become app.get) and you can send it using the fetch API:

fetch("https://blbl.brbr/cities/" + nameInput).then(res => res.json()).then(myData => {/* using myData*/})

The above part is client code; to search into your mysql db from node you can check this page: W3School: Node MySQL


#3

thank I got them connected! I really appreciate it. It’s filtering and everything but instead of giving me all the results it gives me seven duplicates. I tried testing it with different values and it’s always seven repeats of the first data set. Does anyone have any insight?