2017-07-06 100 views
-1

我已经创建了将材质图标切换为开/关的功能。 当图标关闭时,我需要运行删除查询。 当图标打开时,我需要运行插入查询。使用单个链接在MySQL中运行单独的查询

我知道我需要使用AJAX,但对它仍然很陌生。 我无法理解的是我是否引用当前的PHP文件或其他的PHP文件。我知道我必须编写我的查询并在PHP文件中执行它,但不确定要这样做。我不想重新加载页面,因为我这样做会丢失其他信息。

我基本上是想更新图标并执行所需的SQL stmnt。

任何帮助表示赞赏。

我到目前为止有:

JAVASCRIPT:

//update the favorites icon 
function updateFavorites(id){ 
if($(this).find('#staricon'+id)){ 
    if($('#staricon'+id).hasClass('star-color')) { 
     $('#staricon'+id).removeClass('star-color'); 
     //update the table   
     deleteFavorites(); 
    } 
    else { 
     $('#staricon'+id).addClass('star-color'); 
     addFavorites(); 
    } 
} 
} 

//delete the item from the table 

function deleteFavorite(){ 

     $.ajax({ 
      type: "POST", 
      url: "somePHPFile.php", 
      cache: false, 
      data:{id:'#staricon'+id}, 
     }).done(function(msg) { console.log(msg); 
     }); 
} 

PHP:

//check to see if this is a favorite 
$query = "SELECT * FROM favorites WHERE story_id = " . $story_id; 
$result = mysqli_query($conn, $query); 
$is_fav = mysqli_num_rows($result); 

if ($is_fav > 0) { 
echo '<a class=" stats pull-right " href="javacript:void" ><span id="staricon' . $story_id .'" class="star-color" onclick="updateFavorites(' . $story_id . ')"><i class=" material-icons " title="Favorite" >star</i></span></a>'; 
} 
else { 
echo '<a class=" stats pull-right " href="javacript:void" ><span id="staricon' . $story_id .'" onclick="updateFavorites(' . $story_id . ')"><i class=" material-icons " title="Favorite" >star</i></span></a>'; 
} 

我有更新我的代码,以反映如下:

JAVASCRIPT:

function updateFavorites(id){ 
if($(this).find('#staricon'+id)){ 
    if($('#staricon'+id).hasClass('star-color')) { 
     $('#staricon'+id).removeClass('star-color'); 
     $.ajax({ 
      type: "POST", 
      url: "showStoryCards.php", 
      data: { 
       id: $(this).data(id), 
       enabled: !$(this).hasClass('star-color') //delete 
      }, 
     }) 
    } 
    else { 
     $('#staricon'+id).addClass('star-color'); 
     $.ajax({ 
      type: "POST", 
      url: "showStoryCards.php", 
      data: { 
       id: $(this).data("id"), 
       enabled: $(this).hasClass('star-color') //insert 
      }, 
     })    

    } 
} 

PHP:

echo $story_title ; 
$query = "SELECT count(*) FROM favorites WHERE story_id = ?"; 
$sql= $conn->prepare($query); 
$sql->bind_param("s", $story_id); 
$sql->execute(); 
$result = $sql->get_result(); 
$is_fav = mysqli_num_rows($result); 
if ($is_fav == 0) { 
echo '<a class=" stats pull-right " href="javacript:void" ><span 
id="staricon' . $story_id .'" class="star-color" 
onclick="updateFavorites(' . $story_id . ')"><i class=" material-icons " 
title="Favorite" >star</i></span></a>'; 
} 
else { 
echo '<a class=" stats pull-right " href="javacript:void" ><span 
id="staricon' . $story_id .'" onclick="updateFavorites(' . $story_id . 
')"><i class=" material-icons " title="Favorite" >star</i></span></a>'; 
} 
if (isset($_POST['enabled'])){ 

if($_POST['enabled']) { // INSERT query 


    $sql = "INSERT INTO favorites VALUES(" . $id . ", '1') "; 
    $sql->execute(); 
} else {// Delete query 
} 
} 

我的图标更新到适当的开/关的颜色,但我仍然不能得到查询开火。它甚至不会出现回调到PHP页面的功能,因为我无法检索$ _POST。

+0

那么,你想你的脚本?怎么了?你真正的问题是什么?你提到了这样做的方式(使用ajax),并且你的帖子中有这些作品。 –

+0

“somePHPFile.php'''是什么?你的javascript应该传递一个值来指示是否要添加或删除项目,然后你的php脚本可以使用它来确定它是否应该添加或删除一行。 –

+0

现在发生的事情是,图标更新正确(开/关)我不明白的是如何(或在哪里)为表更新或如何访问它们添加php sql stmnts。 – Cesar

回答

0

简化了一切。

$(document).ready(function() { 
 

 
    $('#staricon').on('click', function() { 
 
    // Prevent multiple clicks before the first one finishes. 
 
    // There is probably a more elegant way to do this. 
 
    if(active){ 
 
     return; 
 
    } 
 
    active = false; 
 
    //console.log($(this).hasClass('star-color')); 
 
    //console.log($(this).data("id")); 
 
    $.ajax({ 
 
     type: "POST", 
 
     url: "somePHPFile.php", 
 
     data: { 
 
     id: $(this).data("id"), 
 
     enabled: !$(this).hasClass('star-color') 
 
     }, 
 
    }).done(function(msg) { 
 
     active = true; 
 
     $(this).toggleClass('star-color'); 
 
     console.log(msg); 
 
    }); 
 
    }); 
 

 
}); 
 

 

 
/* 
 
PHP 
 
// Use ID and enabled to add or delete from db. 
 
if($_POST['enabled']) { 
 
    // INSERT query 
 
} else { 
 
    // Delete query 
 
} 
 
*/
div.star-color { 
 
    background-color: #FF00FF; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="staricon" data-id="1" class="star-color">Test</div>

+1

好的解决方案,但我会提供两个更改:_(1)_删除'cache'参数。由于你正在发出'POST'请求,所以它是无关紧要的,_(2)_将'.toggle()'语句移动到'.done()'例程中。这样资源才能在通话成功时切换。否则,您可能会遇到一些状态问题(即星号关闭,但ajax呼叫失败等) – War10ck

+1

完成。我不得不在数据中启用启用的逻辑,但只要你的两端一致,我想它并不重要。 – mkaatman

+0

感谢你对此...但我仍然不明白如何通过单击一个图标来运行单独的查询。我在哪里放置插入查询和删除查询? – Cesar

1

使用准备好的语句,因为你打开自己注入攻击。

尝试此查询

$query = "SELECT * FROM favourites WHERE story_id = ?"; 
$sql= $conn->prepare($query); 
$sql->bind_param("s", $story_id); 
$sql->execute(); 
$result = $sql->getResult(); 
print_r($result);