Janssen Schembri
Janssen Schembri

Reputation: 35

Passing value with POST PHP method

My aim is to delete a row whenever I click delete button. My method doing this is by passing the value of ['movies_id'] and storing it inside a hidden input and then passing it when delete button is pressed. The issue is that when I press the delete button it only reads the first id, which in this case is 1. Even though I press the delete button in the 3 card for example. So, my question is how do I pass the correct 'movies_id' stored inside 'keyToDelete' so that it doesn't always read 1?

As you can see in the image below, the id from mysql is being read well inside the card.

function displayMovies()
{
    global $dbc;
    $movieSelect = "SELECT * FROM movies_tbl";
    $query = mysqli_query($dbc, $movieSelect);

    if (mysqli_num_rows($query) == 0) {

        echo "There is nothing to display.";
    } else {
        $edit = "";
        $delete = "";
        if (isset($_SESSION['type'])) {
            if ($_SESSION['type'] == "admin") {
                $edit = "<a href='#' class='btn btn-cyan align-self-end'>Edit</a>";
                $delete = '<input type = "submit" name="delete" value="Delete" form="movieForm" class="btn btn-cyan align-self-end">';
                while ($row = mysqli_fetch_assoc($query)) {
                    movieDesc($row['movies_id'], $row['movie_title'], $row['main_actor'], $row['movie_length'], $row['average_rating'], $row['release_date'], $row['description'], $row['img_path'], $row['trailer_url'], $edit, $delete);
                }
            } else if ($_SESSION['type'] == "user") {
                while ($row = mysqli_fetch_assoc($query)) {
                    movieDesc($row['movies_id'],$row['movie_title'], $row['main_actor'], $row['movie_length'], $row['average_rating'], $row['release_date'], $row['description'], $row['img_path'], $row['trailer_url'], $edit, $delete);
                }
            }
        } else {
            for ($i = 1; $i < 4; $i++) {
                $row = mysqli_fetch_assoc($query);
                movieDesc($row['movies_id'],$row['movie_title'], $row['main_actor'], $row['movie_length'], $row['average_rating'], $row['release_date'], $row['description'], $row['img_path'], $row['trailer_url'], $edit, $delete);
            }
        }
    }
}

if (isset($_POST['delete'])){
    $key = $_POST['keyToDelete'];
    echo"<h1>$key</h1>";
}

function movieDesc($movies_id,$movie_title, $main_actor, $movie_length, $average_rating, $release_date, $description, $img_path, $trailer_url, $edit, $delete)
{
    echo '<div class="col-md-4 mb-4">
            <div class="card">
                <img src="images/' . $img_path . '.jpg" class="card-img-top cardImage">
                <div class="card-body d-flex flex-column">
                <form action="movies.php" id="movieForm" method="post">
                    <h3 class="card-title text-center fontEDO">' . $movie_title, $movies_id . '</h3>
                    <div class="row">
                        <div class="col">
                            <p class="card-text text-left"><b>Main Actor:</b></p>
                            <p class="card-text text-left"><b>Movie Length:</b></p>
                            <p class="card-text text-left"><b>Average Rating:</b></p>
                            <p class="card-text text-left"><b>Release Date:</b></p>
                        </div>
                        <div class="col">
                            <p class="card-text text-left">' . $main_actor . '</p>
                            <p class="card-text text-left">' . $movie_length . '</p>
                            <p class="card-text text-left">' . $average_rating . '</p>
                            <p class="card-text text-left">' . $release_date . '</p>
                        </div>
                    </div>
                    <p class="card-text text-left description"><br><b>Description:</b> ' . $description . '</p>
                    <input type="text" name="keyToDelete" value='. $movies_id .'>
                    </form>
                    <div class="mt-auto text-center">
                    <a href = "' . $trailer_url . '" target="_blank" class="btn btn-cyan align-self-end">Watch Trailer</a>
                        ' . $edit . '
                        ' . $delete . '
                    </div>
                </div>
            </div>
        </div>';
}

enter image description here

Upvotes: 0

Views: 56

Answers (1)

Barmar
Barmar

Reputation: 780974

You can't have duplicate id="movieForm". When you use form="movieForm" in the submit button, it submits the first form with that ID, not the one just before the button.

You should move the submit button inside the form, and get rid of form="movieForm" from the button.

Or give each form a unique ID, and use that in the form attribute of the submit button.

Upvotes: 3

Related Questions