sedward
sedward

Reputation: 31

Node JS: Pass object from HTML to Server

I'm currently passing in an object into my ECT html file (same as EJS just for coffeescript) which has a form. This object contains a business name, business id and visit id and I only display the business name inside my ECT file.

I'm trying to pass the business object into the body with my post request that contains all the form details. I've tried the whole data-* (http://html5doctor.com/html5-custom-data-attributes/) but that isn't working.

This is what my form looks like right now

<form id="form" action="/answer" method="post" data-biz="<%= @business%>">
# Inputs here

However, my request body only contains the values for my inputs and nothing with the business object. Is there any way to pass my business object back into my post request for /answer?

Thanks!

Upvotes: 1

Views: 311

Answers (1)

mutil
mutil

Reputation: 3305

You can JSON.stringify the object and pass it through a hidden input:

<form id="form" action="/answer" method="post">
  <input name="name" value="<%= @business.name %>"/>
  <input name="business" type="hidden" value="<%= JSON.stringify(@business) %>"/>
</form>

Upvotes: 1

Related Questions