在我的下拉列表中,我放置了用户发布的所有“pack_name(s)”,并将其全部显示在列表中供用户选择和更新。因此,当用户选择一个并点击提交时,我想获取提交的“值”并将其用于以后的目的,但我一直在研究,只找到了带有 html 的“预设”值,并且该值是使用 Jquery 给出的。所以我想知道是否有可能基本上选择“pack_name”,当用户点击提交时,回显所选值。
PHP
<?php
session_start();
if(empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post'){ //catch file overload error...
$postMax = ini_get('post_max_size'); //grab the size limits...
echo "<p style=\"color: #F00;\">\nPlease note files larger than {$postMax} will result in this error!</p>"; // echo out error and solutions...
return $postMax;
}
if(isset($_COOKIE['id'])){
if($_SESSION['came_from_upload'] != true){
setcookie("id", "", time() - 60*60);
$_COOKIE['id'] = "";
header("Location: developerLogin.php");
exit;
}
try{
// new php data object
$handler = new PDO('mysql:host=127.0.0.1;dbname=magicserver', 'root', '');
//ATTR_ERRMODE set to exception
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
die("There was an error connecting to the database");
}
$userid = $_SESSION['id'];
$stmt = $handler->prepare("SELECT * FROM pack_profile WHERE pack_developer_id = :userid");
$stmt->bindParam(':userid', $userid, PDO::PARAM_INT);
$stmt->execute();
echo "<select>";
while($result = $stmt->fetch()){
echo "<option>" . $result['pack_name'] ."</option>";
}
echo "</select>";
if($_SERVER['REQUEST_METHOD'] =="POST"){
$token = $_SESSION['token'];
}
}
?>
最佳答案
您需要提供select
元素a name
属性,并给出每个 option
元素a value
属性。
例如:
echo "<select name=\"pack\">";
while($result = $stmt->fetch()){
echo "<option value=\"" . $result['pack_name'] . "\">" . $result['pack_name'] ."</option>";
}
echo "</select>";
当然,您应该转义任何可能包含 &
的内容。 , <
或"
类似于 htmlspecialchars()
.
关于javascript - PHP echo从html下拉列表中选择的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45558035/