简体   繁体   中英

PHP echo selected value from html dropdown list

In my dropdown list, i put all the "pack_name(s)" the user has posted and I display it all in the list for the user to select and update. So when the user selects one and hits submit, i want to get that "value" submitted and use it for later purposes but ive been researching and only found "pre-set" values with html and the value was given using Jquery. So i wondering if its possible to basically take the "pack_name" selected and when the user hits submit, echo out the selected value.

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'];


}
}


?>

You need to give the select element a name attribute, and give each option element a value attribute.

For example:

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

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

   echo "</select>";

Of course you should be escaping anything which could contain & , < or " with something like htmlspecialchars() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM