douzi6060
douzi6060
2017-02-16 06:58

如何创建一个提交按钮,根据下拉列表中的选定输入显示结果

  • button
  • submit
  • wordpress
  • php
  • mysql

I am using PHP and MySQL database on WordPress.

I had created 4 dropdown list in a table where each one includes data retrieved from the database and I created a submit button.

What I need is to make the submit button takes the selected input from each dropdown list and make a query to return the required data and display the results in a table or a grid.

code:

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

If anyone can help me to continue the code in order to:

  • use the selected data from the dropdown list in the PHP code
  • display the result in a table or grid

After updating the code the page looks like : web page based on the @Prateek Verma code

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐