I'm using click function to get the current ID of the clicked form, then running submit form function to apply my code.
But once I click to run the function the code doesn't apply, I've tried to debug the code to find out what causing this problem, it's seems like the click function works properly, but the submit function doesn't work at all.
The submit function is working if there weren't any click function wrapping it.
<form method="POST" class="<?php echo $row['code']; ?>" id="form<?php echo $row['id']; ?>">
<a id="submit" class="product-links" id="cartsub" href='javascript:addCart(<?php echo $row['id']; ?>);'>
<i class="fa fa-shopping-cart"></i>
</a>
</form>
<script>
function addCart(id){
$("#form" + id).submit(function(e) {
e.preventDefault();
var code = $(this).find('input[id=code]').val();
var dataString = 'code=' + code;
$.ajax({
type: 'POST',
url: 'addcart.php',
data: dataString,
cache: false,
success: function (result) {
if(result == "success"){
alertify.success("המוצר נוסף לעגלה");
$("#plus").fadeIn(300).delay(1500).fadeOut(300);
}
else{
alertify.error("המוצר כבר קיים בעגלה");
}
}
});
$("#cartreload1").load("product.php #cartreload1");
$("#cartreload2").load("product.php #cartreload2");
$("#cartreload1").load("product.php #cartreload1");
$("#cartreload2").load("product.php #cartreload2");
});
}
</script>
How can I implement submit function inside click function?
P.S. I insist on keeping onclick function in html because its valuable for my php code.