dragon_9000 2019-02-02 16:47
浏览 82

无法从PHP获取ID到AJAX

I am trying to build an E-commerce website using PHP and AJAX but I am facing problem when I try to delete items from the cart. I am fetching the cart data from the cart table that is in my database. The fetched data code is shown below. (Please don't care about the variables, they are all in the code I am not pasting here)

echo "<tr>
       <td class='p-image'>
        <a href='product-details?id=$fetchProductID'><img alt='' src='$theRealLink'></a>
       </td>
       <td class='p-name'><a href='product-details?id=$fetchProductID'>$theName</a></td>
       <td class='p-amount'>INR $fetchProductUnitPrice</td>
       <td class='p-quantity'><input maxlength='100' type='text' value='$fetchProductQuantity' name='quantity'></td>
       <td class='p-total'><span>INR $fetchedProductTotal </span></td>
       <td class='edit'><a href='javascript:void(0)' id='delete-from-cart' data-id='$fetchProductID'><img src='assets/img/icon/delte.png' alt=''></a>
       </td>
     </tr>";

Whenever someone presses the button with the ID of delete-from-cart, I run the javascript code that fetched the ID of the product to be deleted from data-id attribute. The code is below

$('#delete-from-cart').click(function() {
    var deleteID = $(this).attr('data-id');
    alert(deleteID);
});

Just alerting for now. The real problem is, say my cart have 3 different items, so I get 3 different buttons to delete them but only the button of the first item in the cart is working i.e. An alert is displayed containing the ID of the product to be deleted.

I am wondering that why only the first button is working and rest all are not working at all ?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思