I’m trying to append a value (quantity) from a text input field to a url. Of course I can’t get this to work.
What I’ve got is:
<form class="formProduct" id="formProduct" action="#" method="post">
<input type="text" name="quantity" id="formProductQuantity" value="{{ product.stock.minimum }}" /> // text field with quantity
<a class="button blue opener" href="" title="{{ 'Add to cart' | t }}"><span>{{ 'Add to cart' | t }}</span></a> // submit button
</form>
<script>
var addUrl = "http://shop.com/cart/add/123456/?quantity="; // 123456 is the product id
asset("#formProduct");
function asset(product){
$(product + " input#formProductQuantity").val("1");
$(product + " #formProductQuantity").keyup(function () {
var val = $(product + " input#formProductQuantity").first().val();
});
$(product + " .opener").click(function() {
var val = $(product + " input#formProductQuantity").first().val();
// Go to page
window.location.href = addUrl + val;
});
}
</script>
So what I try is to pass the quantity filled in the form to pass to http://shop.com/cart/add/123456/?quantity=100 for example.
The code above I found on this forum but I can’t get this to work.
Any help more then welcome
UPDATE
Ok thx to all the help here I got it to work. Any comments or modifications are still more then welcome. Function code looks like this:
<script type="text/javascript">
var addUrl = "http://shop.com/cart/add/123456/?quantity="; // 123456 is the product id
jQuery(document).ready(function(){
asset("#formProduct");
});
function asset(product){
jQuery(product + " input#formProductQuantity").val("1");
jQuery(product + " #formProductQuantity").keyup(function () {
var val = jQuery(product + " input#formProductQuantity").first().val();
});
jQuery(product + " .opener").click(function(event) {
event.preventDefault();
$.get($(this).attr('href'), function(data, status) {
var val = jQuery(product + " input#formProductQuantity").first().val();
// Go to page
window.location.href = addUrl + val;
$( "#dialog" ).dialog( "open" );
return false;
});
})}
You are trying to add Javascript to the submit button so it will run through the Javascript but the form will still be submitted, therefore window.location.href won’t take place. What you need to do is add
to the end of your asset() function:
This will stop the form from submitting, allowing your window.location.href to redirect as you required.