php - 通过 PHP 中的 SQL 查询列出所选标签中的选项

标签 php mysql

我想从 <select> 检索一个值标记为PHP ...此选定标签中的选项是 PHP 中的函数其中列出了来自单独的 php 的这些选项文件名为 functions.php

请参阅 functions.php 中的以下函数

function list_brand (){
  $sql = "SELECT brand FROM laptop
         ";
  $query = mysql_query($sql) or die (mysql_error());

  if ($query) {
     //echo 'success'; <-- testing the function functinality

     while ($query_row = mysql_fetch_assoc($query)) {
           $brand = $query_row['brand'];

           echo '<option value="brand">'.$brand.'</option>';
     }      
  } else {echo 'fiald if';}
}

下面是选定的标签:

<select id="test" onchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text">
    <option value="select">SELECT BRAND</option>
    <?php list_brand(); ?>
</select>
<input type="hidden" name="brand_text" id="text_content" value="" />

现在我的问题是,我还有另外两个选定的标签,它们应该根据第一个选定标签中的选定选项列出选项,并且应该列出完整的 SQL 查询

下面是我的完整 html 代码:

<form action="comparison.php" method="GET">
<table width="80%" border="0" align="center" cellpadding="1" cellspacing="2">
  <tr>
    <td colspan="2">Lap 1</td>
    <td colspan="2">Lap 2</td>
  </tr>
  <tr>
    <td width="19%">Brand</td>
    <td width="31%"><select><option value="brand1">==SELECT BRAND==</option>
                    <?php
                      list_brand();
                    ?>
                    </select></td>
    <td width="19%">Brand</td>
    <td width="31%"><select name="brand2"><option value="brand2">==SELECT BRAND==</option>
                    <?php
                      list_brand();
                    ?>
                    </select></td>
  </tr>
  <tr>
    <td width="19">Model</td>
    <td width="31"><select><option value="model1">==SELECT MODEL==</option></select></td>

    <td width="19">Model</td>
    <td width="31"><select><option value="model2">==SELECT MODEL==</option></select></td>
  </tr>
  <tr>
    <td width="19">Partnumber</td>
    <td width="31"><select><option value="pn1">==SELECT PARTNUMBER==</option></select></td>

    <td width="19">Partnumber</td>
    <td width="31"><select><option value="pn2">==SELECT PARTNUMBER==</option></select></td>
  </tr>
  <tr>
    <td height="453" colspan="2">&nbsp;</td>
    <td colspan="2">&nbsp;</td>
  </tr>
</table>
</form>

最佳答案

就是这个了。我必须在 OnChange 上放置两个函数,一个用于分配所选值,另一个是提交页面以便刷新...

 <form name="index" action="" method="GET">
  <select id="brand" onchange="document.getElementById('brand_content').value=this.options[this.selectedIndex].text; document.index.submit();">
          <option value="">==SELECT BRAND==</option>
                      <?php
                        list_brand();
                      ?>
  </select>
  <input type="hidden" name="brand_text" id="brand_content" value="" />

<?php
      $brand = $_GET['brand_text'];
      if (isset($brand)){

      }  else {echo 'no brand selected';}
?>
  <br>
  <select id="model" onchange="document.getElementById('model_content').value=this.options[this.selectedIndex].text; document.index.submit(); ">>
  <option value="model1">==SELECT MODEL==</option>
  <?php
       list_model($brand);
  ?>
  </select>
  <input type="hidden" name="model_text" id="model_content" value="" 
</form>

关于php - 通过 PHP 中的 SQL 查询列出所选标签中的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16639414/

相关文章:

MySQL左外连接与连接表中的计数,显示所有记录

mysql - 为什么在 MySQL 中引用用户名和主机名?

php - 将 SQL 函数作为 PDO 准备语句运行并返回值

php - select语句在foreach中填充数据

php - Google Analytics API : How to pull accounts, 在非对象错误上获取 listManagementAccounts()

SQL 查询和插入

php - 从数据库生成实体

php - 有没有一种简单的方法使用 CSS 或 Javascript 根据字段的值更改 div 的背景颜色

添加到函数时 PHP $.row 错误

mysql - 嵌套 "select ... in"性能缓慢 - 如何修复?