如何在不重新加载页面的情况下刷新内容(愿望清单)?

时间:2020-08-26 17:12:05

标签: javascript php jquery

我正在通过使用TMDb的API学习PHP。我正在建立一个仍在工作的愿望清单。现在脚本应该变得更加舒适。

我有一个功能,可在每个电影页面上输出,可在其中单击愿望清单。

功能getButtons()

function getButtons() {

    global $movieID;
    echo '<button type="button" class="btn btn-success">Besitze ich</button> ';

    /*
     * Überprüfe, ob der Film bereits in der Wunshcliste verfügbar ist
     */

    $pdo = new PDO(
        'mysql:host=localhost;dbname=moviesdb',
        'root',
        ''
    );

    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $stmt = $pdo->prepare("SELECT * FROM wishlist WHERE user_id = '".$_SESSION['id']."' AND movie_id = '". $_GET['movieID'] ."'");
    $stmt->execute();

    $countEntries = $stmt->rowCount();
    //echo $countEntries;

    if ($countEntries >= 1) {
        echo '<a href="wishlist.php?movieID='. $movieID .'&action=remove"><button type="button" class="btn btn-danger">Von Wunschliste entfernen</button></a> ';
    }
     else {
         echo '<a href="wishlist.php?movieID='. $movieID .'&action=add"><button type="button" class="btn btn-primary">Auf meine Wunschliste</button></a> ';
     }


    echo '<button type="button" class="btn btn-danger">Verliehen an</button>';
    echo '</div>'; // end div col
    echo '</div>'; // end div row
    echo '<hr>';

}

如果用户单击“愿望清单”按钮,则将通过wishlist.php?movieID=XYZ?action=add将其转发到wishlist.php。

wishlist.php

<?php

$pdo = new PDO(
    'mysql:host=localhost;dbname=moviesdb',
    'root',
    ''
);

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$stmt = $pdo->prepare("SELECT * FROM wishlist WHERE user_id = '" . $_SESSION['id'] . "'");
$stmt->execute();

?>

<?php

/*
 * Prüfe, ob überhaupt Einträge vorhanden sind
 */

echo '<p>Hallo ' . $_SESSION['name'] . ', es befinden sich ' . $stmt->rowCount() . ' Einträge in deiner Wunschliste.</p>';

    /*
     * Debug Variablen
     */

//var_dump($_SESSION['id']);
//var_dump($_GET['movieID']);
//var_dump($_GET['action']);

    /*
     * Überprüfe die action
     */

    if (isset($_GET['action'])) {
        if ($_GET['action'] == 'add') {

            $stmt = $pdo->prepare("SELECT * FROM wishlist WHERE user_id = '" . $_SESSION['id'] . "' AND movie_id = '" . $_GET['movieID'] . "'");
            $stmt->execute();

            $countEntries = $stmt->rowCount();
            // echo $countEntries;

            if ($countEntries >= 1) {
                echo '<p>Der Film befindet sich bereits auf deiner Wunschliste!</p>';
            } else {

                $stmt = $pdo->prepare("INSERT INTO wishlist (user_id, movie_id) VALUES (:user_id, :movie_id)");
                $stmt->execute(['user_id' => $_SESSION['id'], 'movie_id' => $_GET['movieID']]);

                echo '<p>Der Film wurde deiner Wunschliste hinzugefügt.</p>';
            }
        }

        if ($_GET['action'] == 'remove') {

            $stmt = $pdo->prepare("DELETE FROM wishlist WHERE user_id = '" . $_SESSION['id'] . "' AND movie_id = '" . $_GET['movieID'] . "'");
            $stmt->execute();

            $countEntries = $stmt->rowCount();
            // echo $countEntries;

            if ($countEntries >= 1) {
                echo '<p>Der Film wurde aus deiner Wunschliste gelöscht.</p>';
            }
        }
    }

    if ($stmt->rowCount() > 0) {

        $stmt = $pdo->prepare("SELECT * FROM wishlist WHERE user_id = '" . $_SESSION['id'] . "'");
        $stmt->execute();

        echo '<table class="table table-striped table-sm">';
        echo '<thead>';
        echo '<tr>';
        echo '<th scope="col">Cover</th>';
        echo '<th scope="col">Titel</th>';
        echo '<th scope="col">Eintrag löschen</th>';
        echo '</tr>';
        echo '</thead>';

        echo '<tbody>';
        foreach ($stmt as $row) {

            $url = "https://api.themoviedb.org/3/movie/" . $row['movie_id'] . "?api_key=" . $apiKey . "&" . $language . "&" . $sortBy . ""; // path to your JSON file
            $data = file_get_contents($url); // put the contents of the file into a variable
            $movie = json_decode($data); // decode the JSON feed

            echo '<tr>';
            echo '<td class="align-middle"><img src="https://image.tmdb.org/t/p/w45/' . $movie->poster_path . '" class="img-thumbnail"></td>';
            echo '<td class="align-middle"><a href="getMovie.php?movieID=' . $row['movie_id'] . '">' . $movie->title . ' (' . substr($movie->release_date, 0, 4) . ')</a></td>';
            echo '<td class="align-middle"><a href="wishlist.php?movieID=' . $row['movie_id'] . '&action=remove"><span class="badge badge-danger">Entfernen</span></a></td>';
            echo '<tr>';
        }

        echo '</tbody>';
        echo '</table>';

    }

?>

<?php include 'footer.php'; ?>

问题:如果要使更改可见,我总是必须在浏览器中重新加载页面。我该如何解决?

1 个答案:

答案 0 :(得分:1)

尝试使用javascript或javascript框架,可以使用jquery进行请求并动态显示数据。创建一个js函数,以在按钮单击之类的事件触发时获取并显示它们。

<script>
function getdata(){
$.get("http://example.com/fetchdata", function(data,status){
for(){
 //loop your data


}
});
}
</script>
相关问题