没有页面刷新的切换按钮[关闭]

问题描述 投票:0回答:0

我正在建立一个电影网站并有一个我正在使用 Javascript 的“我的列表”按钮 - 我不希望在播放电影预告片时刷新页面。提交表单后,例如将电影添加到“我的列表”,它会调用该函数并更新数据库(到目前为止一切正常)。从那里我想在两个功能之间切换 - 从我的列表中添加或删除电影。那就是它停止工作的地方。

表单按钮

<?php if ( !isset( $_POST[ 'favorite' ] ) && $title_fav != "") {?>
  <div id="fav_delete" style="display: inline-block;">
    <form style="display: inline-block;" id="my_list_delete">
      <input style="display: none" type="text" name="title" value="<?php echo $title; ?>" />
      <input style="display: none" name="favorite_delete" value="favorite" />
      <input id="btn_mylist_on" type="submit" value="MY LIST">
    </form>
</div>
<?php } ?>
            
<?php if ( !isset( $_POST[ 'favorite' ] ) && $title_fav == "") {?>
  <div id="fav_add" style="display: inline-block;">
    <form style="display: inline-block;" id="my_list_add">
      <input style="display: none" type="text" name="title" value="<?php echo $title; ?>" />
      <input style="display: none" name="favorite" value="favorite" />
      <input id="btn_mylist_default" type="submit" value="MY LIST">
    </form>
  </div>    
<?php } ?>

JavaScript 函数

<script> 
$(function () {
    $('#my_list_add').bind('click', function (event) {
      event.preventDefault();
      $.ajax({
        type: 'POST',
        url: 'ajax/mylist.php',
        data: $('form').serialize(),
        success: function (data) {
          $('#fav_add').html("<span id=\"btn_mylist_on\" style=\"cursor: text;\">MY LIST</span>");
        }
      });
    });
  });
$(function () {
  $('#my_list_delete').bind('click', function (event) {
    event.preventDefault();
    $.ajax({
      type: 'POST',
      url: 'ajax/mylist.php',
      data: $('form').serialize(),
      success: function (data) {
        $('#fav_delete').html("<span id=\"btn_mylist_default\" style=\"cursor: text;\">MY LIST</span>");
      }
    });
  });
});
</script>

mylist.php

<?php if ( isset( $_POST[ 'favorite' ] )) {
    $sql_favorites = "INSERT INTO favorites (title, username) VALUES ('$title_keyword', '$username')"; 
    mysqli_query($db_connect, $sql_favorites);
    } ?>

<?php if ( isset( $_POST[ 'favorite_delete' ] )) {
    $sql_favorites = "DELETE FROM favorites WHERE title = '$title_keyword' AND username = '$username'"; 
    mysqli_query($db_connect, $sql_favorites);
    } ?>
javascript php ajax
© www.soinside.com 2019 - 2024. All rights reserved.