dqx13503925528 2015-12-28 18:40
浏览 37
已采纳

只能用Ajax删除第一行

I have a page that prints out rows from a mysql table. I'm trying to create an ajax form that allows users to delete rows but for some reason I can only seem to get it to delete the very top row that is printed out. I've only included the script that might be needed here and left out the database query(which works fine).Firebug only shows my form being posted when I click the top row of results, any other rows it does nothing. Can anyone tell me what's wrong? Thanks

My_reviews.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
  </script>

<script type="text/javascript">
//Delete Review
$(document).ready(function(){
$("#deleteReview").click(function (e) {
        e.preventDefault();


 var username=$("#username").val();
 var film_id=$("#film_id").val();
 var id=$("#id").val();


 $.post('ajax_deleteReview.php', {username: username, film_id:    film_id, id: id},
 function(data){
$("#message").html(data);
 $("#message").hide();
$("#message").fadeIn(500);
$("#message").fadeOut(2500); 
});
return false;
});
});
</script>
</head>

<div class="container">
<div id="message"></div>
<?php
  $sql = "SELECT * FROM user_reviews WHERE username='$username' ORDER BY DATE desc";
$result = $db_conx->query($sql);
 while($row = $result->fetch_assoc()) {
$id = $row['id']; 
$film_id = $row['film_id']; 
$review = $row['review']; 
$movie = $tmdb->getMovie ($film_id);

echo ' 
<div class="row">
        <div class="col-md-1">

<a href="film_info.php?film_id='. $movie->getID() .'"><img id="image1" src="'. $tmdb->getImageURL('w150') . $movie->getPoster() .'" width="80" /></a>
<p>
</p>
</div>
  <div class="col-md-4">
          <h3>
             ' . $movie->getTitle() .'  
          </h3>';
echo'
          <p>
            '.$review. '
          </p>

<form>
    <input type="hidden" id="username" name="username" value="'. $username.'">
<input type="hidden" id="film_id" name="film_id" value="'.$film_id .'">
<input type="hidden" id="id" name="id" value="'.$id .'">
<button type="submit" id="deleteReview" class="btn btn-danger btn-xs pull-right">delete</button>
</form>
        </div>
        <div class="col-md-7">
        </div>
      </div>';

}
?>

    <script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js">
      </script>
    </div>
    </body>
    </html>

ajax_deleteReview.php

<?php
//include db configuration file
include_once("ajax_review/config.php");
//Configure and Connect to the Databse
 $username=$_POST['username'];
 $film_id=$_POST['film_id'];
 $id=$_POST['id'];
//Delete Data from Database
 $delete_row = $mysqli->query("DELETE * FROM `user_reviews` WHERE     id='$id' AND username='$username' AND film_id='$film_id' LIMIT 1");
if($delete_row){
echo '<img src="images/tick_large.png"/>';
 }
 else{ echo "An error occurred!"; }
 ?>
  • 写回答

1条回答 默认 最新

  • duanpiyao2734 2015-12-28 19:58
    关注

    You have duplicated IDs in inputs, so jQuery returns the 1st occurrence of input.
    You can add film_id, id and film_name to the <a> link with a data attribute, then read with jquery.
    JavaScript function for ajax request need to be assigned to the class:

    $(".deleteReview").click(function (e) {...
    

    and

    <a class="deleteReview"....
    

    so with this you eliminate duplicate IDs in HTML code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘