1

I have the following ejs file and I want to capture the NAME field value inorder to fetch the respective details from database and display onto another page. I know how to do this using form tag, but I am unsure how to capture in this case. Any help would be appreciated. Thanks.

search.ejs file:

<a href="/menu/<%= tablelist[i].NAME %>">
                <img alt="<%= tablelist[i].NAME %>" class="restaurant-card-cover" src="<%= tablelist[i].IMAGELINK %>" style="height: 115px;">
                    <div class="restaurant-card-content-text">
                        <div id="catererName" class="restaurant-card-name heading-color" variable="<%= tablelist[i].NAME %>"><%= tablelist[i].NAME %></div>
                            <div class="card-description"><%= tablelist[i].DESC2 %></div></a>

In app.js file, I added the following:

app.get('/menu/:name', routes.menu(ibmdb,connString));

And, I have another folder "routes" and I am mentioning all my SQL queries in index.js file. Here it is:

    exports.menu = function(ibmdb,connString) {
return function(req, res) {

var name = req.param.name;

 ibmdb.open(connString, function(err, conn) {
        if (err ) {
         res.send("error occurred " + err.message);
        }
        else {

            conn.query("SELECT ITEM_NAME, PRICE FROM HOMEBASEDAPP.MENU WHERE CATERER_NAME='"+name+"';", function(err, tables, moreResultSets) {


            if ( !err ) { 
                res.render('menu', {"tablelist" : tables, name: name});

            } else {
               res.send("error occurred " + err.message);
            }

            /*
                Close the connection to the database
                param 1: The callback function to execute on completion of close function.
            */
            conn.close(function(){
                console.log("Connection Closed");
                    });
            });
        }
    } );

}
}

I tried to print the "name" in menu.ejs file but its not getting the value.

Naeem Shaikh
  • 15,331
  • 6
  • 50
  • 88
Ann
  • 15
  • 7
  • You don't even open the form, but you do have a closing tag. Please fix your indentation and make your HTML more readable. – Ruben Rutten Nov 26 '14 at 19:43

1 Answers1

0

This is done on the frontend.

One way to do that would be to add a data tag, say on the same div where you want to capture the click. I will take this div from you, but you can adapt:

<div class="restaurant-card-name heading-color"><%= table[i].NAME %></div>

Now, add a data tag, something we can put data in:

<div class="restaurant-card-name heading-color" data-restaurant="<%= table[i].NAME %>">
    <%= table[i].NAME %>
</div>

Now, your div will render the name as a data attribute.

Then you have some click handler. Assuming jQuery here, you can grab a click on such divs. This bit javascript loaded when the dom is ready should do it:

$('.restaurant-card-name').click(function(evt) {

    evt.preventDefault();
    var name = this.data('restaurant');
    $.ajax({
      url: '/where/you/want/to/fetch/stuff/from',
      method: 'POST',
      // other stuff here, like displaying the data or image in a modal or similar.
    })
 });

Alternatively, if you want to just navigate to another page, as you say, there are a few steps.

First one is to prepare the url instead of the data attribute:

<a href="/my-restaurant-handler-route/<%= table[i].NAME %>">
    <div class="restaurant-card-name heading-color">
        <%= table[i].NAME %>
    </div>
</a>

Now, your href value will be, for example, /my-restaurant-handler-route/mcdonalds

The second step is to prepare a route on the server. Since you're using ejs and you've tagged Node, I'll assume you're using express.

You need to prepare a route handler like this:

app.get('/my-restaurant-handler-route/:name', function(req, res) {

    // param is on the request, whatever you named it after the `:` in app.get.
    var name = req.param.name;
    // now do the db dance
    database.getData({restaurantName: name}).then(function(data) {

       // you have the data, put it on the response or render it:
       res.render('restaurant-handler.ejs', {data: data});
    });
});

That is one way to do it. There are others, but you'll have to specify your situation a bit more for more details.

Zlatko
  • 18,936
  • 14
  • 70
  • 123
  • Thanks for your detailed reply. Appreciate that. I tried but its not getting the value "name" to pass into the database. I have edited my question to include more details. Thanks. – Ann Nov 27 '14 at 00:27
  • Ouch! Db.open on every request? Nope, do db.open at the start of the module/file, leave it open (use an ORM?), then just reuse it. What's in menu.ejs? Also, before that db call, can you at least log the query as string, does it show correctly? – Zlatko Nov 27 '14 at 01:02
  • menu.ejs just displays the results from the db query which has the "name" column in Where clause. But, its not displaying any results since its not getting the value of "name". – Ann Nov 27 '14 at 02:15
  • Oh, now I see it's a typo: it's not req.param, it is req.params :) – Zlatko Nov 27 '14 at 11:08
  • Zlatko, I figured that out after editing my ques.. But anyways, thank you so much for your help. Your suggestion was really helpful and I am able to pass the name field now. But I got another problem because of this change. After adding the name attribute at the end of /menu, my css is not working in menu.ejs page. Its not taking css at all and displays the content one below the other. Do you know why?? – Ann Nov 27 '14 at 18:48
  • For that one, I'd really need to see at least a part of your menu.ejs. I'm guessing that you're not extending some core layout, like you are doing in the search.ejs. That actually may be another question. – Zlatko Nov 28 '14 at 13:06
  • In menu.ejs, as of now, I am just printing the restaurant name like this:

    Caterer Name

    <%= name %>

    along with bootstrap header and footer navbar.
    – Ann Nov 29 '14 at 01:51