下拉值以及指向其他页面的超链接

时间:2015-03-11 02:28:48

标签: php

下面的代码使用sql server database中的值动态填充下拉列表。

是否可以创建值的超链接,以便在从下拉列表中选择任何选项时,它会将所选值传递给另一个页面,例如anwers.php?

<select name="Types" style="width:178px;">
 <option value=""></option>

 <?php
     $tsql = "select st.wills,st.types sc from [status][s]
left outer join [Wills][st] on s.id=st.wills
where st.types is not null and st.wills=1
group by st.types, st.wills
union all
select s.id,s.Status sc from [status][s]
left outer join [Wills][st] on s.id=st.wills where st.wills <> 8
group by s.id,s.Status";
     $stmt = sqlsrv_query( $conn, $tsql);
     if( $stmt === false )
     {
      echo "Error in executing query.</br>";
      die( print_r( sqlsrv_errors(), true));
     }
      while($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)){
       echo '<option value="' . $row['sc'] . '" name="' . $row['sc']. '">' . $row['sc']. '</option>';
     }
  ?>
 </select>

谢谢

更新:

<form method="get" action="answer.php" target="SearchResults">
 <select id="Types" name="Types" style="width:178px;">
 <option value=""></option>
 <option value="wills" name="wills">wills</option>
 <option value="Library" name="Library">Library</option>
 <option value="gates" name="gates">gates</option>
 <option value="danes" name="danes"></option>
 <option value="Reviews" name="Reviews">Reviews</option>
 <option value="Closed" name="Closed">Closed</option>
 <option value="Pending" name="Pending">Pending</option></select>
</form>

1 个答案:

答案 0 :(得分:0)

您可以将选择内容包装在表单中,如下所示:

<form id="FormTypes" method="get" action="answer.php">
    <select name="Types" id="Types" style="width:178px;">
    (...)
    </select>
</form>

并在onchange事件中提交表单:

<script type="text/javascript">
   window.onload = function() {
       document.getElementById("Types").onchange = function() {
           document.getElementById("FormTypes").submit();
       }
   } 
</script>

请注意,您必须将id和name属性添加到select标记才能生效。