dongyihao1099 2017-08-30 14:59
浏览 67

如何使用jquery ajax在循环中提交表单

I tried submitting the forms without jquery Ajax and it worked perfectly well. But when I apply jquery Ajax, it submits only the first form in the loop. I have tried giving them a different id but its still not working. I don't know if I've missed it somewhere. Please help me out. This is my php code containing the form.

<?php
$i = 0;
while($row = $products->fetch(PDO::FETCH_ASSOC)){
$image = $row['productimage'];
$price = $row['productprice'];
$id = $row['productid'];
$name = $row['productname'];
echo '<form method="post" class="cart" id="form'.$i.'">';
echo '<div class="col-sm-3">';
echo '<div class="product-image-wrapper">';
echo '<div class="single-products">';
echo '<div class="productinfo text-center">';
echo '<img src="'.$image.'" />';
echo '<input type="hidden" name="amount" value=1>';
echo '<input id="input" class="price" name="price" value="'.$price.'" 
readonly>';
echo '<input type="hidden" name="productid" value="'.$id.'">';  
echo '<p>'.$name.'</p>';
echo '<input name="submit"  class="btn btn-default add-to-cart" name="addto"   
type="submit" value="Add to cart">
</div></div>
</div>
</div>
</form>';
$i++;               
};
?>

And this is my jquery Ajax script

$(document).ready(function(){
$(".cart").on('submit', function(e) {
e.preventDefault();
$.ajax({
url: 'cartprocess.php', 
type: "POST",
data: $(this).serialize(),
success: function(data){
$("#error").html("Updating...").delay(1000).fadeOut(1000, function(){
$("#error").fadeIn(1000).html("updated!");
});
},

});
});
});
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器