donglu0494 2016-04-17 04:01
浏览 157
已采纳

提交按钮在div中不起作用(其内容在jQuery中使用.load()加载)

cart.php items.phpThe following code displays products in the cart of a user.using the submit buttons user can delete individual items in the cart.It is saved in cart.php file.This code works fine.

<?php
session_start();
?>
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
</head>
<body>
    <div class="txt-heading">Shopping Cart</div>
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>

</tr>   
 <?php
        $servername = "localhost";
$username = "root";
$password = "";
$dbname = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

      $ip = $_SERVER['REMOTE_ADDR'];

         $sql = "SELECT * FROM cart where ip='".$ip."'";
         $result = mysqli_query($conn, $sql);
    $item_total =0;

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        ?><form method="post" action="" id="cart1">
        <tr> 
        <td><strong><?php echo $row["name"]; ?></strong></td>
        <td><?php echo $row["qty"]; ?></td>
        <td align=right><?php echo "Rs.".$row["price"]; ?></td>
  <input name="no" type="hidden" value="<?php echo $row["orderno"] ?>" />   
 <td><input type="submit" name="submit" id="submit"/></td>
  </tr>
    </form>

                <?php
       $item_total += ($row["price"]*$row["qty"]);
        }
 } 







 ?> 
 <?php if ($_POST['submit']) {
    $or=$_POST['no'];
     $sql = "delete FROM cart where orderno='".$or."'";
         $result = mysqli_query($conn, $sql);
    header("Location: ".$_SERVER['PHP_SELF']);}
    ?>

<tr>

<td colspan="5" align=right><strong>Total:</strong> <?php echo     "Rs.".$item_total; ?></td>
</tr>
</tbody>
</table>        

</body>
</html>

The problem is that when i load this into a div of another page items.php using

  <script>
$(document).ready(function(){
setInterval(function(){
$("#cart").load('cart.php')
}, 20);
});

</script>

The cart.php is properly loading in the div #cart, but when i click on submit nothing happens. Please note that on clicking submit in cart.php the product was deleted from the database.

Can anybody please explain how to fix this problem?

  • 写回答

2条回答 默认 最新

  • douzhen9428 2016-04-17 04:05
    关注

    I presume the click handler is getting attached but the DOM(the submit button in your case) is not present , so you may need to delegate the click event like this

    $('body').on('click','#submit',function(event){ // submit is id of submit button
      // rest of the code
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大