PHP从html下拉列表中回显选定的值

时间:2017-08-08 01:40:13

标签: javascript php jquery html

在我的下拉列表中,我将所有" 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'];


}
}


?>

1 个答案:

答案 0 :(得分:1)

您需要为select元素提供name属性,并为每个option元素提供value属性。

例如:

   echo "<select name=\"pack\">";

   while($result = $stmt->fetch()){
     echo "<option value=\"" . $result['pack_name'] . "\">" . $result['pack_name'] ."</option>";
   }

   echo "</select>";

当然,您应该使用&之类的内容转义包含<"htmlspecialchars()的任何内容。