从php的列表框中选择值

pxq42qpu  于 2022-12-17  发布在  PHP
关注(0)|答案(3)|浏览(154)

我想要的值,我从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>
6l7fqoea

6l7fqoea1#

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

访问参数中的值selectedvalue(val)
获取所选值并将其存储在input type= hidden中,因为您需要它以供进一步使用。

0mkxixxg

0mkxixxg2#

如果你想保存到php-variable中,你必须发布表单。我在下面添加了一个submit按钮。你还必须指定一个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>

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

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

如果你想在不需要重新加载页面的情况下发布表单,你必须使用一种叫做 AJAX 的东西。

byqmnocz

byqmnocz3#

这对我很有效。试试这个例子。

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

相关问题