0

I am a Web Development student who is struggling with an exercise we were given on our first week importing XML data into a simple Node.JS project.

For the exercise, I need to bring in data from an XML file of library branches, use it to populate a drop down menu, that once submitted will take the user to a new page that displays information of a specific branch based on the selected option tag's submitted ID value.

I was able to get the concept working with simple anchor redirection, but once I brought a form into the mix I've had no success.

Here is a simplified version of my form. I haven't been sure if I set the action parameter correctly:

  <form method="get" action="/branch/">
    <select name="branchSelect" id="branchSelect">
      <option value="id1">Branch 1</option>
      <option value="id2">Branch 2</option>
    </select>
    <input type="submit" value="Confirm"/>
  </form>

And here is the current code in my index.js to get to the page I want to reach

app.get("/branch/:id", (req, res) => {

  let id = req.params.id; 
  let branch = getBranchById(id);
  let title = branch.querySelector("name").textContent;
  res.render("branch", {title: title, branch: branch})

});

I'm fairly certain my issue has to do with how I've set my form action and how the branch ID data in my GET URL accessed/interpreted after form submission. This post (How to access the GET parameters after "?" in Express?) seems to show a solution to a similar problem, but using req.query.id over req.params.id in my javascript hasn't worked for me.

I'd appreciate any advice that can be offered. I'm new at this, so apologies if this is something simple I'm just missing.

JDevenyi
  • 101
  • 6

1 Answers1

0

I figured out the issue. I needed to have the action and app.get be pointed at the same path THEN use a req.query to grab the id in the url.

JDevenyi
  • 101
  • 6