如何获取 HTML 和 PHP 中的输入值

问题描述 投票:0回答:1

我正在尝试创建更新模式,但我无法从名为 update_player_id 的隐藏输入中获取player_id。

我尝试使用$_GET和$_POST,但没有成功。我将输入从隐藏更改为文本和player_id,它已正确传递。

$_GET 和 $_POST 的 var_dump 是 Array(0) { },如果我尝试使 $player_id = $_GET['update_player_id'] 我有以下输出:警告:未定义的数组键“Update_player_id”,与 $ 相同玩家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>
    
php html
1个回答
0
投票
完成

David评论后,您可能会在执行任何表单请求之前尝试阅读update_player_id

首先,如果您使用

<input type="hidden" name="update_player_id" value="">

提交,您将不会有任何数据,因为属性
name
为空。

因此,您需要先获取

player id

,无论它来自其他页面、按钮等,因为您无法从上面的输入中获取它,然后您可以在显示表单并填写之前搜索
player
它与玩家信息。

<?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); // ...
最后,您可以使用 

heredoc 使字段生成更清晰,而无需 echo

 语句,并且可以选择使用 
array destructuring
 代替 
$row["key"]

© www.soinside.com 2019 - 2024. All rights reserved.