簡體   English   中英

將選定的值保留在下拉菜單中PHP HTML

[英]Keeping selected value in drop down menu PHP HTML

我有下面的代碼,但是每次我選擇一個值並且頁面刷新下拉菜單中的值時,它都會恢復為默認值,如何保存所選值。

我不能使用AJAX,因為用戶不希望那樣做。

我確實在網上搜索,發現我需要添加“ selected”,但是當我在while語句上這樣做時,它不起作用。

這是我到目前為止使用的代碼

<h4>Please select a Scheme name</h4>
<br>
<form name="export_form" action="<?php echo($_SERVER['PHP_SELF']);?>" method="post">
  <select name="schemaNameSelect" id='schema_name_dropdown_menu'>

    <option value='default' id='default' >Please select scheme name</option>
    <option value='ViewAll' id='ViewAll' >View All</option></center>

    <?php
    while($dbRow = sqlsrv_fetch_array($dbQuery, SQLSRV_FETCH_ASSOC)) {
      echo "<option value='".$dbRow[Schema_Name]."'>". $dbRow[Schema_Name] . " </option>";
    }

    echo "</select>";
  }
  ?>
  <a  id="viewButton" type="submit"><button id="viewBtn">View</button></a>
  <input type="submit" name="submit_docs" value="Export your certificate" id="exportBtn" class="input-button" />

  <div id="test">
    <br/>
    <input id="create_schema_wrapper_input" name="schemaNameTextbox" data-id="12" class="stage3-input" placeholder="Please insert new schema name here">
  </div>

</form>



<form name="export_form" action="<?php echo($_SERVER['PHP_SELF']);?>" method="post">

</form>
<table class="table table-striped" id="student">
  <tr>
    <th>Schema Name</th><th>Stakeholder</th><th>Stakeholder Return</th><th>Schema Return</th>
  </tr>

  <?php 
  if (isset($_POST['schemaNameSelect'])) {
    $schemaName = $_POST['schemaNameSelect'];
    databaseOutput($schemaName); 
  } else {
    databaseOutput("ViewAll"); 
  }
  ?>
</table>
</body>

<style>
#schema_name_dropdown_menu{
  padding: 5px;
  font-size: 14px;
  width: 350px;
  height: 35px;
  margin: 5px;
}
#test{
  display: none;
}
#viewButton{
  font-size: 13px;
  text-decoration: none;
  color: black;
  display: none;
}

#viewButton:hover {
  font-size: 15px;
  color: blue;
  text-decoration: none;
}
#viewBtn{
  border-radius: 5px;
  margin-left: 5px; 
  font-size: 14px;
  width: 60px;
  height: 35px;
  margin: 5px;
}
#exportBtn{
  margin: 5px;
  height: 35px;
  width: 150px;
  font-size: 14px;
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript">
  $('#schema_name_dropdown_menu').change(function() {
    if ($(this).val() == 'default')
    {
      $('#viewButton').hide();
    }else{
      $('#viewButton').show();
      $('#test').hide();
      var value = $("#schema_name_dropdown_menu option:selected").val();
      $('#test').text(value);
    }
  });
</script>

在填充下拉列表的while循環中,檢查是否已選擇下拉列表,然后將selected屬性添加到在while循環中找到的選項

<h4>Please select a Scheme name</h4>
<br>
<form name="export_form" action="<?php echo($_SERVER['PHP_SELF']);?>" method="post">
  <select name="schemaNameSelect" id='schema_name_dropdown_menu'>

    <option value='default' id='default' >Please select scheme name</option>
    <option value='ViewAll' id='ViewAll' >View All</option></center>

    <?php
    while($dbRow = sqlsrv_fetch_array($dbQuery, SQLSRV_FETCH_ASSOC)) {

        // code to add the selected attribute
        $sel = '';
        if (    isset($_POST['schemaNameSelect']) 
            &&  $_POST['schemaNameSelect'] == $dbRow[Schema_Name] ) 
        {
            $sel = 'selected';
        }


        echo "<option $sel value='".$dbRow[Schema_Name]."'>". $dbRow[Schema_Name] . " </option>";
        // change     ^^^
    }

    echo "</select>";
  }
  ?>
  <a  id="viewButton" type="submit"><button id="viewBtn">View</button></a>
  <input type="submit" name="submit_docs" value="Export your certificate" id="exportBtn" class="input-button" />

  <div id="test">
    <br/>
    <input id="create_schema_wrapper_input" name="schemaNameTextbox" data-id="12" class="stage3-input" placeholder="Please insert new schema name here">
  </div>

</form>



<form name="export_form" action="<?php echo($_SERVER['PHP_SELF']);?>" method="post">

</form>
<table class="table table-striped" id="student">
  <tr>
    <th>Schema Name</th><th>Stakeholder</th><th>Stakeholder Return</th><th>Schema Return</th>
  </tr>

  <?php 
  if (isset($_POST['schemaNameSelect'])) {
    $schemaName = $_POST['schemaNameSelect'];
    databaseOutput($schemaName); 
  } else {
    databaseOutput("ViewAll"); 
  }
  ?>
</table>
</body>
    <select name="select">
        <option <?=$variable1 == $variable2 ? 'selected' : ''?> value="1">One</option>
    </select>

暫無
暫無

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

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