在php中从列表框中选择的值

在php中从列表框中选择的值,php,listbox,selectedvalue,Php,Listbox,Selectedvalue,我想要从列表框中选择的值,并希望将其存储在php变量中,因为我希望使用该变量以供进一步使用。请帮忙 <form name="myform" method="post"> <?php include('dbconnect.php'); db_connect(); $query = "SELECT * FROM test_customer"; //Write a query $data = mysql_query($query);

我想要从列表框中选择的值,并希望将其存储在php变量中,因为我希望使用该变量以供进一步使用。请帮忙

<form name="myform" method="post">
   <?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>
</form>
访问在参数中选择的值ValueVal


获取所选值并将其存储在input type=hidden中,因为您需要它以供进一步使用。

如果要存储到php变量中,则必须发布表单。我在下面添加了一个提交按钮。您还必须指定表单提交到的操作:action=whatever.php

  <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>
如果您想在不重新加载页面的情况下发布表单,则必须使用ajax。

这对我很有用。请尝试此示例


使用$\u POST处理表单后,为什么不将其保存在php变量中?是否有不带onchange事件的选项?您能否演示如何将该值存储在php变量中?您想发布该变量吗?或者你想如何/为什么使用它?因此,我可以简化代码我想在另一个查询中传递该值,以便根据该值生成另一个列表框。使用AJAX..这样你就可以在更改选择时更改选择列表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
$cust_name = $_POST['mylist']; //mylist is the name of the select-list
?>
 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
          $__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);
    ?>