2017-07-06 92 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);