I am having this html code:
<form class="add-to-cart-form">
<input type="hidden" name="product-code" value="2" />
<input type="submit" value="Add" class="button" onclick="addToCart('2');"/>
</form>
And a javascript code:
function addToCart(product_id, quantity) {
quantity = typeof(quantity) != 'undefined' ? quantity : 1;
$.ajax({
url: 'test.php',
type: 'post',
data: 'product_id=' + product_id + '&quantity=' + quantity,
dataType: 'json',
success: function(json) {
alert(json.message);
}
});
}
IN the html file anytime, I click on the submit button, instead of taking me to the function addToCart, the form is submitted.
I am new to javascript and really don't know the source of the problem. Kindly help me solve this problem.
submittobuttonreturn falseto onclick attribute