skip to Main Content

I’m trying to make an update modal, but i can’t get the player_id from the hidden input named update_player_id.

I tried to use $_GET and $_POST, but it did not work. I changed input from hidden to text and player_id it’s passed correctly.

var_dump of $_GET and $_POST are Array(0) { } and if I’m trying to make $player_id = $_GET[‘update_player_id’] i have this output: Warning: Undefined Array Key "Update_player_id", same for $player_id = $_POST[‘update_player_id’]

<div id="update-player-modal" class="update-player-modal">
    <div class="modal-content">
        <span class="close" onclick="closeUpdatePlayerModal()">&times;</span>
        <h2>Update Player</h2>
        <div class="form">
            <form action="process-update-player.php" method="POST">
                <input type="hidden" name="update_player_id" value="">                

                <?php
                echo $_GET['update_player_id'];
                $player_id = $_GET['update_player_id'];
                                        
                $mysqli = require "../database/database.php"; 
                $sql = "SELECT * FROM player WHERE id = ?";
                $stmt = $mysqli->prepare($sql);
                    
                $stmt->bind_param("i", $player_id);
                    
                $stmt->execute();
                    
                $result = $stmt->get_result();
                    
                if ($result->num_rows > 0) {
                    
                    while ($row = $result->fetch_assoc()) {
                        echo "<div class='fullname'>";
                        echo "<div>";
                        echo "<label for='firstName'>First Name</label>";
                        echo "<input type='text' id='firstName' name='firstName' value='" . $row["firstName"] . "'>";
                        echo "</div>";
                        echo "<div>";
                        echo "<label for='lastName'>Last Name</label>";
                        echo "<input type='text' id='lastName' name='lastName' value='" . $row["lastName"] . "'>";
                        echo "</div>";
                        echo "</div>";
                            
                        echo "<div>";
                        echo "<label for='birthday'>Birthday</label>";
                        echo "<input type='text' id='birthday' name='birthday' value='" . $row["birthday"] . "'>";
                        echo "</div>";
                            
                        echo "<div>";
                        echo "<label for='nationality'>Nationality</label>";
                        echo "<input type='text' id='nationality' name='nationality' value='" . $row["nationality"] . "'>";
                        echo "</div>";
                            
                        echo "<div>";
                        echo "<label for='alias'>Alias</label>";
                        echo "<input type='text' id='alias' name='alias' value='" . $row["alias"] . "'>";
                        echo "</div>";
                            
                        echo "<div>";
                        echo "<label for='position'>Position</label>";
                        echo "<input type='text' id='position' name='position' value='" . $row["position"] . "'>";
                        echo "</div>";
                            
                        echo "<div>";
                        echo "<label for='team'>Team</label>";
                        echo "<input type='text' id='team' name='team' value='" . $row["team"] . "'>";
                        echo "</div>";
                    }
                } else {
                    echo "Player not found!";
                }
                    
                $stmt->close();
                $mysqli->close();
                ?>
                <div class="update-player-button">
                    <button>Update</button>
                </div>
            </form>
        </div>     
    </div>
</div>

2

Answers


  1. Completing David comment, you’re probably trying to read update_player_id before doing any form request.

    First, if you submit with <input type="hidden" name="update_player_id" value="">, you won’t have any data, because the attribute name is empty.

    So, you need to get the player id first, wherever it comes from another page, button, etc. because you can’t get it from the input above, then you can search the player before show the form and fill it with the player info.

    <?php
        // ...
        
        // This value could get from the url http://....?update_player_id=4
        $player_id = $_GET['update_player_id']; // -> 4
                                            
        $mysqli = require "../database/database.php"; 
        $sql = "SELECT * FROM player WHERE id = ?";
        $stmt = $mysqli->prepare($sql);
    
        // ...
    

    Finally, you could use a heredoc to make the field generation cleaner without echo statements and optionally array destructuring instead of $row["key"].

    Login or Signup to reply.
    • You can not perform that action on a modal in that way because the entire page will be reloaded which defeats the purpose of the modal.
    • But, you may still do so and then use JavaScript code to automatically launch the modal after the page have loaded. You may use your openUpdatePlayerModal() with jQuery’s document.ready() function.
    • If you choose to do it that way, you have to include the id of the player that you want to edit in that form. Precisely, it should be the value of the update_player_id hidden field.
    • So when the page modal launches, the id is hidden with only a submit button. When the button is clicked, it submit’s the player id via POST, so you have to retreive the value via $_POST['update_player_id'].
    • You also have to include the value of the update_player_id field, in the edit form, in with a hidden field. This is so that you would retrieve it again to know the player id that is being updated.

    Your code should look like this:

    
    <div id="update-player-modal" class="update-player-modal">
        <div class="modal-content">
            <span class="close" onclick="closeUpdatePlayerModal()">&times;</span>
            <h2>Update Player</h2>
            <div class="form">
                <form action="process-update-player.php" method="POST">
                    <input type="hidden" name="update_player_id" value="PLAYER_ID_SELECTED_FOR_UPDATE">                
    
                    <?php
                    echo $_POST['update_player_id'];
                    $player_id = $_POST['update_player_id'];
                                            
                    $mysqli = require "../database/database.php"; 
                    $sql = "SELECT * FROM player WHERE id = ?";
                    $stmt = $mysqli->prepare($sql);
                        
                    $stmt->bind_param("i", $player_id);
                        
                    $stmt->execute();
                        
                    $result = $stmt->get_result();
                        
                    if ($result->num_rows > 0) {
                        
                        while ($row = $result->fetch_assoc()) {
    
                            echo "<div class='fullname'>";
                            echo "<input type='HIDDEN' id='update_player_id' name='update_player_id' value='" . $row["DATBASE_ROW_ID"] . "'>";
                            echo "<div>";
                            echo "<label for='firstName'>First Name</label>";
                            echo "<input type='text' id='firstName' name='firstName' value='" . $row["firstName"] . "'>";
                            echo "</div>";
                            echo "<div>";
                            echo "<label for='lastName'>Last Name</label>";
                            echo "<input type='text' id='lastName' name='lastName' value='" . $row["lastName"] . "'>";
                            echo "</div>";
                            echo "</div>";
                                
                            echo "<div>";
                            echo "<label for='birthday'>Birthday</label>";
                            echo "<input type='text' id='birthday' name='birthday' value='" . $row["birthday"] . "'>";
                            echo "</div>";
                                
                            echo "<div>";
                            echo "<label for='nationality'>Nationality</label>";
                            echo "<input type='text' id='nationality' name='nationality' value='" . $row["nationality"] . "'>";
                            echo "</div>";
                                
                            echo "<div>";
                            echo "<label for='alias'>Alias</label>";
                            echo "<input type='text' id='alias' name='alias' value='" . $row["alias"] . "'>";
                            echo "</div>";
                                
                            echo "<div>";
                            echo "<label for='position'>Position</label>";
                            echo "<input type='text' id='position' name='position' value='" . $row["position"] . "'>";
                            echo "</div>";
                                
                            echo "<div>";
                            echo "<label for='team'>Team</label>";
                            echo "<input type='text' id='team' name='team' value='" . $row["team"] . "'>";
                            echo "</div>";
                        }
                    } else {
                        echo "Player not found!";
                    }
                        
                    $stmt->close();
                    $mysqli->close();
                    ?>
                    <div class="update-player-button">
                        <button>Update</button>
                    </div>
                </form>
            </div>     
        </div>
    </div>
    

    Good luck.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search