shoppingcart page html forms design - html

while designing the template for a shoppingcart view,I used a loop to create a list of items in the cart to be displayed .I also want to provide remove buttons to enable the user to remove each item from the cart.The html came out to be like this
here in the below html code,in /books/removefromcart/91 , 91 is the shoppingcart id.
The cartitemid represents the item's id.
Is this bad style to create one form for each list item?Which is the correct way?
<form action="/books/removefromcart/91" method="POST" >
<li>1. mastering java 1 copies
<input type="hidden" name="cartitemId" value="94"/>
<input type="submit" value="remove"/>
</li>
</form>
<form action="/books/removefromcart/91" method="POST" >
<li>2. mastering python 2 copies
<input type="hidden" name="cartitemId" value="93"/>
<input type="submit" value="remove"/>
</li>
</form>
<form action="/books/removefromcart/91" method="POST" >
<li>3. mastering ruby 1 copies
<input type="hidden" name="cartitemId" value="92"/>
<input type="submit" value="remove"/>
</li>
</form>
..
<!--a form for adding item to cart -->
<form action="/books/addtocart/43" method="POST">
<label for="quantity">Number of Copies:</label>
<select id="quantity" name="quantity">
<option selected="" value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
...
</select>
<input type="hidden" name="cartId" value="91"/>
<input type="submit" value="AddToCart" />
</form>

On each button of update/remove call one javascript function and pass that product value to that function...and then you can process on it...
e.g: ---
<input type="button" OnSubmit="remove('91');" value="Remove" />
function remove(id)
{
$.ajax({
url: 'yourpage.aspx,
type: 'POST',
data: 'prodid='+id,
success: function(response) {
/*your code for success goes here */
}
});
}

Related

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

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

How to Submit Multiple Values in a single HTML Form?

So I have a HTML form:
<html>
<body>
<script>history.pushState('', '', '/')</script>
<form action="http://myserver.com" method="POST">
<input type="hidden" name="Id" value="83" />
<input type="hidden" name="url" value="http://example.com/" />
<input type="submit" value="Submit request" />
</form>
<script>
document.forms[0].submit();
</script>
</body>
</html>
As you can see this is submitting the action for <input type="hidden" name="Id" value="83" /> meaning it's submitted for the attribute associated with ID number 83, I'm wanting the action to be submitted for multiple ID values, i.e. 1 - 100. Is this possible? If so how can it be done?
I assume you want to do something like this
<html>
<body>
<script>history.pushState('', '', '/')</script>
<form action="http://myserver.com" method="POST">
<input type="hidden" name="Id[]" value="83" />
<input type="hidden" name="Id[]" value="85" />
<!-- you can add as many as input here for id if you want -->
<input type="hidden" name="url" value="http://example.com/" />
<input type="submit" value="Submit request" />
</form>
<script>
document.forms[0].submit();
</script>
</body>
</html>
After this form is posted, on the server side you can get $_POST['id'] as an array and playing around with it.
Add [] to input name:
<input type="hidden" name="ID[1]" value="83" />
<input type="hidden" name="ID[100]" value="100" />
then the in php
print_r($_POST['ID']); //print out the data array
Or use just one input with comma separated values?
<input type="hidden" name=Id value="1, 2, 3,.., 100" />
PHP:
$ids = explode(" ", $_POST['ID']);
Old post, but like to add a Flask solution.
Flask can handle 'name' tags inside the form fields.
<div class="modal-body">
<p>Do you want to add {{ jar.jar_name }} Jar to Map</p>
<div class="col">
<form action="{{ url_for('hornet._jar_on_map')}}">
<input type="hidden" value="{{jar.jar_name}}" name="jar_name"/>
<select class="form-select form-select-lg mb-3" aria-label=".form-select-lg" name="map_name">
{% for map in maps %}
<option value='{{map.map_name}}'>{{ map.map_name }}</option>
{% endfor %}
</select>
<button type="submit" class="btn btn-secondary">Yes</button>
</form>
</div>
The submit button returns to a Flask route '_jar_on_map'. This calls a function to add an item on a map. It is the request.args that shall read your name tags of the different values. Then you can handle those tags inside your function. I made a dict again.
#hornet_bp.route("/add_jar_on_map", methods=["POST", "GET"])
def _jar_on_map():
returneddata = {}
print(request.args)
returneddata["jar_name"] = request.args.get('jar_name')
returneddata["map_name"] = request.args.get('map_name')
print(f"-------------------{returneddata['jar_name']}")
print(f"-------------------{returneddata['map_name']}")
jar = Hornet.find_one_by_name(jar_name=returneddata["jar_name"])
if jar:
update = Hornet.bind_to_map(bind_jar_to_map=returneddata)
if update:
flash(f"Adding Map {returneddata['map_name']} for item {returneddata['jar_name']} OK")
else:
flash(f"Adding Map {returneddata['map_name']} for item {returneddata['jar_name']} FAILED - Does it exist?")
return redirect(url_for(".table_jars"))
By doing document.forms[0].submit(); you are submitting all the input values in that form and values will be saved as Id=83&url=http://example.com/
If you want to submit several forms then you could use a for loop
x = document.forms.length //your desired number or number of forms
for(i = 0; i<x; i++){
document.forms[i].submit();
}

Window.close script doesn't seem to close

Having trouble getting this code to actually close a window. Can anyone help? When the "return" is clicked it just goes to a blank page.
<form method="post" action="http://ww8.aitsafe.com/cf/add.cfm" target="newWindow" onsubmit="window.open('', 'newWindow','width=800,height=500,'+'scrollbars=yes,status=no,toolbar=no,menubar=no,top=200,left=200')">
<p>
<input type="hidden" name="userid" value="A8403475">
<input type="hidden" name="return" value="window.close">
<input type="hidden" name="thumb" value="logo.png">
<select name="productpr">
<option>- Select Size -</option>
<option value="Size one:9.99">Size one - 9.99</option>
<option value="Size two:12.99">Size two - 12.99</option>
</select>
<input type="submit" value="Add to Cart">
</p>
</form>
Defining your code as a value of hidden input field will do nothing in this case.
Create a button and do something similar to this: <button type="button" onclick="window.close();">Close</button>
You didn't specify when you want the window to be closed...

HTML dropdown list issues

I'm having issues with the following code:
<form id="form_shower">
<select id="myselect">
<option value="" selected="selected"></option>
<option value="form_name1">Remove the Association</option>
<option value="form_name2">Copy and associate to new form group</option>
<option value="form_name3">Maintain the old association</option>
</select>
</form>
<form name="form_name1" id="form_name1" style="display:none">
Do stuff that removes the association!
</form>
<form name="form_name2" id="form_name2" style="display:none">
New Internal Form Name: <input type="text" name="newinternalformname" value="">
</form>
<form name="form_name3" id="form_name3" style="display:none">
Do stuff to maintain the old association!
</form>
<script>
$("#myselect").on("change", function () {
$("#" + $(this).val()).show().siblings();
})
</script>
Basically, when I select one of the options from the dropdown list, I get what I want (as of now its just test text) but when I select a new option from the list, it appends the data instead of getting rid of what is associated with one of the previously selected options.
It's because the form that contains the dropdown is also a sibling of the form you are showing. Also you have nested forms, which is not allowed
// The first one in this set is always the form with the dropdown
$("#" + $(this).val()).show().siblings()
I would do the following
$("#myselect").on("change", function () {
$(".show-hide").hide();
$("#" + $(this).val()).show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form_shower">
<select id="myselect">
<option value="" selected="selected"></option>
<option value="form_name1">Remove the Association</option>
<option value="form_name2">Copy and associate to new form group</option>
<option value="form_name3">Maintain the old association</option>
</select>
</form>
<form class="show-hide" name="form_name1" id="form_name1" style="display:none">
Do stuff that removes the association!
</form>
<form class="show-hide" name="form_name2" id="form_name2" style="display:none">
New Internal Form Name: <input type="text" name="newinternalformname" value="">
</form>
<form class="show-hide" name="form_name3" id="form_name3" style="display:none">
Do stuff to maintain the old association!
</form>

how to include directions in iframe

I have the following form:
<form method="get" action="http://maps.google.com/maps" target="_blank">
<select name="daddr">
<option selected value="Bovetstrasse 1, 3012 Bern, Switzerland">Missione (a Berna)</option>
<option value="">Zollikofen</option>
<option value="">Bümpliz</option>
<option value="">Ostermundigen</option>
<option value="">Münsingen</option>
<option value="">Konolfingen</option>
<option value="">Worb</option>
</select>
<input type="text" name="saddr" maxlength="255" value="">
<input type="hidden" name="hl" value="it">
<input type="hidden" name="z" value="15">
<input type="hidden" name="t" value="m">
<input type="hidden" name="layer" value="t">
<input type="hidden" name="doflg" value="ptk">
<!--input type="hidden" name="output" value="embed"-->
<button type="submit">Guidami</button>
<p class="Didascalia">Powered by Google</p>
</form>
It opens a new window with the map and the directions on the left side as usual.
What I would like to do is to open this content in an iframe that I have in the same page but
if I symply change the target to the iframe name, I get an error (This content cannot be displayed in a frame. To help protect the security of information you enter into this website, the publisher of this content does not allow it to be displayed in a frame.)
if I also un-comment the output=embed parameter, then only the map is shown with the route but no listing of directions
Does any-one know how to do this?