簡體   English   中英

PHP分配選擇語句以下拉列表值

[英]PHP assign select statement to drop down list values

我有一個下拉列表,其中填充了數據庫中的值。 我試圖實現的是為每個值分配一個選擇語句。

菜單

填充菜單代碼

<?php
require 'conn.php';

$filter=mysql_query("select distinct fuel_type from car");
while($row = mysql_fetch_array($filter)) {
$options .="<option>" . $row['fuel_type'] . "</option>";

$menu="<form id='filter' name='filter' method='post' action=''>
  <p><label>Filter</label></p>
    <select name='filter' id='filter'>
      " . $options . "
    </select>
</form>";
}
echo $menu;
?>

現在,此菜單將用於過濾汽車列表,並將其放置在側邊欄上 汽車

如何從下拉列表中為每個值分配不同的select語句; select * from car where fuel_type = (dropdown list value selected)無需提交表單的情況下, select * from car where fuel_type = (dropdown list value selected)

也許像下面的東西?

    if($_GET["filter"] == 'Petrol'){
$query=mysql_query("select * from car where fuel_type = 'Petrol'");
header("Location: cars.php");
}

首先,在構建選項標簽時,請將您要在SQL查詢中進行過濾的值分配給value屬性:

$options .="<option value='".$row['fuel_type']."'>" . $row['fuel_type'] . "</option>";

其次,為您的表單和選擇元素分配唯一的ID。 然后將表單提交操作分配給select元素onchange事件。

$menu="<form id='filterForm' name='filterForm' method='post' action=''>
  <p><label>Filter</label></p>
    <select name='filter' id='filter' onchange='document.getElementById("filterForm").submit()'>
      " . $options . "
    </select>
</form>";
}

現在,當您更改選擇選項時,將提交表單並將所選的選項值填充為選擇元素#filter的值。

在表單發布到的php頁面上,檢查ID為#filter的select元素的值以構建查詢。

您需要使用javascript打開帶有某些參數的php頁面。

在php中,您可以從post接收這些參數或獲取super globals,然后根據您收到的參數使用switch語句執行不同的查詢。

關於“沒有提交按鈕”,您可以在javascript中設置一個偵聽器,以在值更改時提交表單或觸發函數。 因為它是一個下拉列表,所以它是一個簡單的內聯onmouseup =“ javascript:submitform();” 就足夠了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM