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

时间:2014-02-08 07:55:46

标签: php listbox selectedvalue

我想要从listbox中选择并希望存储在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>

3 个答案:

答案 0 :(得分:0)

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

在参数

中访问值selectedvalue(val)

获取所选值并将其存储在输入类型=隐藏中,因为您需要它以供进一步使用..

答案 1 :(得分:0)

如果要存储到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>

创建一个名为whatever.php的php文件,在此文件中,通过执行以下操作将值存储到$cust_name

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

如果您想在不重新加载页面的情况下发布表单,则必须使用名为ajax的内容。

答案 2 :(得分:0)

这对我有用。试试这个例子。

 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>');
        }
    }

在您的html表单页面中 $__typKy = $_RA['typeky'] //此行从数据库中获取所选值,并将其设置为函数getAptTypesBySelectedKy()的参数

    <?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);
    ?>