我在WordPress上使用PHP和MySQL數據庫。如何創建一個提交按鈕,顯示基於從下拉列表中選擇的輸入的結果
我在表中創建了4個下拉列表,其中每個列表包含從數據庫中檢索的數據,並創建了一個提交按鈕。
我需要的是使提交按鈕從每個下拉列表中選擇輸入,並進行查詢以返回所需數據並將結果顯示在表格或網格中。
代碼:
<?php
/*
Template Name: search info
*/
get_header();
?>
<?php
// code for submit button ation
if(isset($_POST['submit']))
{
//needed code
$site_name=$_POST['site_name'];
$owner_name=$_POST['owner_name'];
$company_name=$_POST['Company_name'];
$Subcontractor_name=$_POST['Subcontractor_name'];
?>
<table width="30%" >
<tr>
<td>Site Name</td>
<td>Owner Name</td>
<td>Company Name</td>
<td>Subcontractor Name</td>
</tr>
<tr>
<td><?php echo $site_name ; ?></td>
<td><?php echo $owner_name ; ?></td>
<td><?php echo $company_name ; ?></td>
<td><?php echo $Subcontractor_name ; ?></td>
</tr>
</table>
<?php } ?>
<!--create dropdown list site names-->
<form method = "POST" action = ''>
<table width="30%">
<tr>
<td>Site Name</td>
<td>Owner Name</td>
<td>Company Name</td>
<td>Subcontractor Name</td>
</tr>
<tr>
<td><select id="site_name" name = 'site_name'>
<?php
$query_site_name =$wpdb->get_results ("select DISTINCT siteNAME from site_info");
foreach($query_site_name as $site_name)
{
$site_name = (array)$site_name;
echo "<option value = '{".$site_name ['siteNAME']."}'>". $site_name['siteNAME']."</option>";
}
?>
<!--create dropdown list owner names-->
</select></td>
<td><select id="owner_name" name = 'owner_name'>
<?php
$query_owner_name =$wpdb->get_results ("select DISTINCT ownerNAME from owner_info");
foreach($query_owner_name as $owner_name)
{
$owner_name = (array)$owner_name;
echo "<option value = '{".$owner_name ['ownerNAME']."}'>". $owner_name['ownerNAME']."</option>";
}
?>
</select></td>
<!--create dropdown list Company names-->
</select></td>
<td><select id="Company_name" name = 'Company_name'>
<?php
$query_Company_name =$wpdb->get_results ("select DISTINCT companyNAME from company_info");
foreach($query_Company_name as $Company_name)
{
$Company_name = (array)$Company_name;
echo "<option value = '{".$Company_name ['companyNAME']."}'>". $Company_name['companyNAME']."</option>";
}
?>
</select></td>
<!--create dropdown list Subcontractor names-->
</select></td>
<td><select id="Subcontractor_name" name = 'Subcontractor_name'>
<?php
$query_Subcontractor_name =$wpdb->get_results ("select DISTINCT subcontractorNAME from subcontractor_info");
foreach($query_Subcontractor_name as $Subcontractor_name)
{
$Subcontractor_name = (array)$Subcontractor_name;
echo "<option value = '{".$Subcontractor_name ['subcontractorNAME']."}'>". $Subcontractor_name['subcontractorNAME']."</option>";
}
?>
</select></td>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td>
<input type = "submit" name="submit" value = "Search">
</td>
</tr>
</table>
</form>
<?php
get_footer();
?>
如果有人能幫助我繼續代碼爲:
- 使用從下拉列表中選擇數據在PHP代碼
- 顯示結果在表格或網格中
更新代碼後,頁面如下所示: web page based on the @Prateek Verma code
我使用你的代碼後,這是頁面看起來像...看看我更新的問題 –
其中是開放<?你的代碼中的get_footer()函數之前的PHP代碼? –
我的錯誤,我忘了添加在我的問題,但它存在於代碼 –