Interpolate variable inside "each" loop in Handlebars [duplicate] - html

This question already has answers here:
Access a variable outside the scope of a Handlebars.js each loop
(4 answers)
Closed 11 months ago.
I am rendering this in an express app which uses handlebars as template engine:
res.render("movies", { data: pages, arraypages:arrayPages, movie:movie})
"arrayPages" is an array:
[
1, 2, 3, 4,
5, 6, 7
]
"movie" is a string like "top gun"
Inside the handlebars template there is:
{{#each arraypages}}
<form action="/getmoviepage" method="post">
<a>{{this}}</a>
<input type="hidden" name="{{movie}}" value="{{this}}">
</form>
{{/each}}
Which outputs:
<form action="/getmoviepage" method="post">
<a>1</a>
<input type="hidden" name="" value="1">
</form>
<form action="/getmoviepage" method="post">
<a>2</a>
<input type="hidden" name="" value="2">
</form>
<form action="/getmoviepage" method="post">
<a>3</a>
<input type="hidden" name="" value="3">
</form>
<form action="/getmoviepage" method="post">
<a>4</a>
<input type="hidden" name="" value="4">
</form>
<form action="/getmoviepage" method="post">
<a>5</a>
<input type="hidden" name="" value="5">
</form>
<form action="/getmoviepage" method="post">
<a>6</a>
<input type="hidden" name="" value="6">
</form>
<form action="/getmoviepage" method="post">
<a>7</a>
<input type="hidden" name="" value="7">
</form>
As you can see the "movie" variable is not rendered (the variable should render inside the "name" attribute). If I put the variable outside the "each" loop is rendered OK.
{{movie}} //This is rendered OK, outside the "each" loop
{{#each arraypages}}
<form action="/getmoviepage" method="post">
<a>{{this}}</a>
<input type="hidden" name="{{movie}}" value="{{this}}">
</form>
{{/each}}
How do I render a variable inside a "each" loop in handlebars?

Try
<input type="hidden" name="{{../movie}}" value="{{this}}">
The ../ path segment references the parent template scope that should be what you want.
Potential Duplicate Question

Related

How to set different url into <form>, using parameter

I want to call different controllers, depending on start parameter:
(parameter start is boolean)
I am try to implement this task, using thymeleaf framework, but in this case him not working:
<div class="w3-container" align="center">
<input class="w3-input w3-border" name="competition" type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for competitions.." title="Type in a competition name">
<form th:if="${start}" action="/challenger" method="post">
<form th:if="${!start}" action="/competition" method="post">
.............
........
<input class="w3-button" type="submit" value="Go"/>
</form>
</form>
</div>
How to accomplish this task in my case?

post not send form data

I have a problem with sending posts in codeigniter, reading other posts I set the variable max_input_vars = 1000. but does not send the data.
the resulting in html is:
<form id="0" action="CO_controller" method="post">
<input id="idric_0" value="0.02508800 154401490122">
<input id="name_0" value="val0">
<input id="per_0" value="10">
<input id="unit_0" value="g">
<input id="ric_0" value="0.02508800 1544014901">
<input id="command0">
<input id="mod0" type="submit" value="Modific" onclick="document.getElementById('command0').value = 'modific';">
<input id="eli0" type="submit" value="Deleta" onclick="document.getElementById('command0').value = 'deleta';">
<input id="id_sal0" value="0.02508800 1544014901">
</form>
<form id="1" action="CO_controller" method="post">
<input id="idric_1" value="0.02508800 154401490122">
<input id="name_1" value="val0">
<input id="per_1" value="10">
<input id="unit_1" value="g">
<input id="ric_1" value="0.02508800 1544014901">
<input id="command1">
<input id="mod1" type="submit" value="Modific" onclick="document.getElementById('command1').value = 'modific';">
<input id="eli1" type="submit" value="Deleta" onclick="document.getElementById('command1').value = 'deleta';">
<input id="id_sal1" value="0.02508800 1544014901">
</form>
the operation is correct ie when I click the button, set the value of the command and submit.
in debug, I go to see the variable $ _POST and it is empty
As said in the comment, you should use name instead of id. By using id you are not passing the values correctly.
Unless you have a route defined so that CO_controller points to a method (and not just a controller) this is not going to work:
<form id="0" action="CO_controller" method="post">
Your form action should point directly to the method within the CO_controller controller which will process the form input.
Assuming the method within the controller is called process_form your form should point to:
<form id="0" action="CO_controller/process_form" method="post">
give it a go and let us know how it works

form post action going to wrong URL

I have another personal website I want to post my form to, see code
<form name="requestPrint" action="www.mysite1.com/set_var.cgi"
method="POST">
<input name="value" type="hidden" size="8" value="1">
<input type="hidden" name="page" value="mypage.html"/>
<input type="hidden" name="index" value="94"/>
<input type="submit" value ="Print">
</form>
However when the form posts it goes to www.mysite2.com/www.mysite1.com/set_var.cgi
Is there a way to point it to just www.mysite1.com/set_var.cgi?
You should try the following:
action="http://www.mysite1.com/set_var.cgi"

Passing multiple values in JSP url

I am trying to pass multiple values in a JSP page url to another one.
The code is:
<form name="QuantityForm" onsubmit="return quantityValidation()" action='<%="basket.jsp?addItem="+product.PID%>' method="post">
Quantity <input type="text" name="quantity" size="5">
<input class="button button2" type="submit" value="Add to basket" />
</form>
How do i pass the value of quantity(the field of name="quantity") in the same URL? i know it's something like "Search.jsp?item=<%=search%>&item2=value2&item3=value3.." but i can't seem to structure it properly. Thanks
Do not put your ? and parameters in the action url. Put your parameters in input tags:
<form name="QuantityForm" onsubmit="return quantityValidation()" action='basket.jsp' method="post">
<input type="hidden" name="addItem" value="%product.PID%">
Quantity <input type="text" name="quantity" size="5">
<input class="button button2" type="submit" value="Add to basket" />
</form>
you're using method="post" which doesn't allow parameters to be passed into the url. change it to method="get" if you want the parameters to be passed along with the url.

How to keep a customer on the same page after they click "add to cart"

I'm attempting to create my first business webpage. I would like to add a message box where they can submit a personalized message. My question is, how can I code it so they can enter the message and submit it, but not be redirected to their shopping cart (paypal)?
This is the code as of now:
<!-- START SAMPLE PAYPAL FORM 5 -->
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post" style="margin: 0px">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="website#email.com">
<input type="hidden" name="item_name" value="Custom Message">
<input type="hidden" name="custom" value="">
<input type="hidden" name="return" value="http://your-domain.com/thanks-payment.htm">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="quanity" value="1">
<input type="hidden" name="add" value="1">
<input type="hidden" name="amount" value="0.01">
<!-- START COMMENTS LINES -->
<input type="hidden" name="on0" value="Custom Message">Enter Item Comments:<br>
<TEXTAREA ROWS="4" COLS="17" name="os0">
</textarea><br>
<!-- END COMMENTS LINES -->
<input type="image" src="http://www.paypalobjects.com/en_US/i/btn/x-click-but22.gif" border="0" name="submit" alt="Submit Quote" vspace="4"><br>
</form>
<!-- END SAMPLE PAYPAL FORM 5 -->
A simple way,
Look here for basic jQuery UI Modal Window:
http://jqueryui.com/dialog/
And here to make the ajax call to the server with jQuery:
http://api.jquery.com/jquery.ajax/
See this Stackoverflow question for an example:
How Do I Create a multi submit form with no page reload?