【问题标题】:selected value from the listbox in php从 php 的列表框中选择的值
【发布时间】:2014-02-08 07:55:45
【问题描述】:

我想要从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>

【问题讨论】:

  • 使用 $_POST 处理表单后,为什么不将其保存在 php 变量中?

标签: php listbox selectedvalue


【解决方案1】:
<select id="cust_name" name="mylist" onchange="selectedvalue(this.value)">

在参数中访问该值 selectedvalue(val)

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

【讨论】:

  • 有没有没有 onchange 事件的选项?你能告诉我如何将该值存储在 php 变量中吗?
  • 你想发布那个变量?或者你如何/为什么要使用它?所以我可以简化代码
  • 我想在另一个查询中传递该值,以便我可以制作另一个列表框。
  • 使用 AJAX。所以你可以在 selection_list_1 更改时更改 selection_list_2 ..
  • 先生,我没听明白,实际上我想要另一个具有相同客户名称的州列表框,然后是城市
【解决方案2】:

如果要存储到 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 的东西。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多