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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度