Faz887766
Faz887766

Reputation: 29

Keeping selected value in drop down menu PHP HTML

I have the below code but every time I pick a value and the page refreshes the values inside the drop down menu revert back to default, how Can I keep the selected values.

I can not use AJAX as the user does not want that.

I did search online and found out that I need to add "selected" but when I do that on the while statement it does not work.

here is the code I have used so far

<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>

Upvotes: 0

Views: 130

Answers (2)

RiggsFolly
RiggsFolly

Reputation: 94682

In your while loop that fills the dropdown, check if the dropdown has been selected and then add the selected attribute to the option as it is found in the while loop

<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>

Upvotes: 1

kajab
kajab

Reputation: 79

    <select name="select">
        <option <?=$variable1 == $variable2 ? 'selected' : ''?> value="1">One</option>
    </select>

Upvotes: 0

Related Questions