selected value from the listbox in php



PHP Snippet 1:

<select id="cust_name" name="mylist" onchange="selectedvalue(this.value)">

PHP Snippet 2:

  <form name="myform" method="post" action="whatever.php"> <!-- added action here -->   

  <?php
     include('dbconnect.php');
     db_connect();

     $query = "SELECT * FROM test_customer"; //Write a query
     $data = mysql_query($query);  //Execute the query
     ?>
     <select id="cust_name" name="mylist" onchange="selectedvalue()">
     <?php
     while($fetch_options = mysql_fetch_array($data)) { //Loop all the options retrieved from the query
     ?>
     //Added Id for Options Element 
     <option id ="<?php echo $fetch_options['test_id']; ?>"  value="<?php echo   $fetch_options['cust_name']; ?>"><?php echo $fetch_options['cust_name']; ?></option><!--Echo  out options-->

     <?php
     }
     ?>
     </select>

     <!-- added submit button -->
     <input type="submit" value="ok" />

     </form>

PHP Snippet 3:

<?php
$cust_name = $_POST['mylist']; //mylist is the name of the select-list
?>

PHP Snippet 4:

 public function getAptTypesBySelectedKy($valKy) {
        $stmt = "SELECT ap_typ_ky, ap_typ_desc FROM apt_types WHERE ap_stat=1 order by ap_typ_desc";
        try {
            $con = DataHandler::connect();
            $values = $con->prepare($stmt);
            if ($values->execute()) {
                echo '<select class="form-control" name="type" id="apt_types_slc">';
                while ($row = $values->fetch(PDO::FETCH_ASSOC)) {
                    if ($valKy === $row['ap_typ_ky']) {
                        echo '<option value="' . $row['ap_typ_ky'] . '" selected>' . $row['ap_typ_desc'] . '</option>';
                    } else {
                         echo '<option value="' . $row['ap_typ_ky'] . '">' . $row['ap_typ_desc'] . '</option>';
                    }
                }
                echo '</select>';
            }
        } catch (PDOException $ex) {
            echo 'Error on apartment types';
            $error = $ex;
            print_r('<pre>' . $ex->getCode() . '</pre>');
            print_r('<pre>' . $ex->getMessage() . '</pre>');
        }
    }

PHP Snippet 5:

    <?php
          $__typKy;
          if (isset($_RA)) {
              $__typKy = $_RA['typeky'];// you need to find this key from database.
              //this is the selected value key of `<select>`
              }
              $var = new DataHandler();
              $var->getAptTypesBySelectedKy($__typKy);
    ?>