我已經創建了將材質圖標切換爲開/關的功能。 當圖標關閉時,我需要運行刪除查詢。 當圖標打開時,我需要運行插入查詢。使用單個鏈接在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。
那麼,你想你的腳本?怎麼了?你真正的問題是什麼?你提到了這樣做的方式(使用ajax),並且你的帖子中有這些作品。 –
「somePHPFile.php'''是什麼?你的javascript應該傳遞一個值來指示是否要添加或刪除項目,然後你的php腳本可以使用它來確定它是否應該添加或刪除一行。 –
現在發生的事情是,圖標更新正確(開/關)我不明白的是如何(或在哪裏)爲表更新或如何訪問它們添加php sql stmnts。 – Cesar