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

问题描述 投票:0回答:3

我想要从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>
php listbox selectedvalue
3个回答
0
投票
<select id="cust_name" name="mylist" onchange="selectedvalue(this.value)">

在参数中访问值selectedvalue(val)

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


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的内容。


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);
    ?>
© www.soinside.com 2019 - 2024. All rights reserved.