javascript - JQUERY post method always return to first table
问题描述
im start to learn jquery ajax, so i modify my web to ajax but its still error when passing data, its success to acces but always return to first column
here my code for call a value:
while ($r=mysql_fetch_array($query)) {
<a id="tambahkan-cart" value='.$r[id_product].' data-toggle="modal" href="#modal-one" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
}
here my code for calling with click
$("#tambahkan-cart").click(function() {
event.preventDefault();
var id_product = $(this).attr("value");
$.ajax({
url: 'tambah-cart.php',
type: 'POST',
data: {
id_product: id_product
},
success: function(data) {
$("#modal-body").html(data);
}
});
});
here tambah-cart.php.. i need to pass the data here,, but its return to value='1',.. I want to pass data dinamically
$idp=$_POST['id_product'];
$q=mysql_query("SELECT * from `product` WHERE id_product='$idp' ");
im appreciate if you tell me where is my wrong ... i used different value for different id_product when click, but its return to first column,,,.. thanks
解决方案
I thought I would draw your attention to this code too. It has to do with the event
Change this
$("#tambahkan-cart").click(function() {
event.preventDefault();
var id_product = $(this).attr("value");
$.ajax({
url: 'tambah-cart.php',
type: 'POST',
data: {
id_product: id_product
},
success: function(data) {
$("#modal-body").html(data);
}
});
});
To
$("#tambahkan-cart").click(function(event) {
event.preventDefault();
var id_product = $(this).attr("value");
$.ajax({
url: 'tambah-cart.php',
type: 'POST',
data: {
id_product: id_product
},
success: function(data) {
$("#modal-body").html(data);
}
});
});
推荐阅读
- python - 运行 Opencv 时出现问题:“AttributeError: module 'cv2.dnn' has no attribute 'readNet'”
- c# - 如何在redis数据库中配置实例(文件夹)
- netsuite - 在现场帮助 Netsuite 中添加链接
- javascript - 如果有效负载为空,如何使节点响应错误
- java - 带有“new”的构造函数与使用“this()”的构造函数有何不同?
- regex - 正则表达式在第五次出现“\”反斜杠后忽略所有文本
- pandas - 在 Pandas 中减去日期时间对象
- python - Docker:从源代码构建 psycopg2 需要 pg_config
- javascript - 得到插值({{}}),其中表达式应为角度 8
- python - 大熊猫月底前的几天