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()">×</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
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 attributename
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 theplayer
before show the form and fill it with the player info.Finally, you could use a heredoc to make the field generation cleaner without
echo
statements and optionallyarray destructuring
instead of$row["key"]
.openUpdatePlayerModal()
with jQuery’sdocument.ready()
function.$_POST['update_player_id']
.Your code should look like this:
Good luck.