1

Please guide me to get the selected option in a drop-down to the PHP variable

<div class="form-group">
    <label class="control-label" for="input-order-type"><?php echo $entry_order_type; ?></label>
    <select name="filter_order_type" id="input-order-type" class="form-control">
      <option value=""></option>
      <option value="<?php echo $filter_order_type; ?>">FB Orders</option>
      <option value="<?php echo $filter_order_type; ?>">Direct Orders</option>
      <option value="<?php echo $filter_order_type; ?>">Khathija's Orders</option>
      <option value="<?php echo $filter_order_type; ?>">Ameer's Orders</option>
      <option value="<?php echo $filter_order_type; ?>">Saheed's Orders</option>
      <option value="<?php echo $filter_order_type; ?>">Nada's Orders</option>
      <option value="<?php echo $filter_order_type; ?>">Henath's Orders</option>
      <option value="<?php echo $filter_order_type; ?>">Noha's Orders</option>
    </select>
    <?php echo $filter_order_type; ?>
</div>

I need the selected value to be assigned in $filter_order_type

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Ask xah
  • 31
  • 1
  • 1
  • 4

3 Answers3

0

The only way this can be done in PHP is by submitting the form by user and then reading the submitted value by using $_POST or $_GET variable. This is explained in this answer: https://stackoverflow.com/a/17139538/4264695

krzysiej
  • 889
  • 9
  • 22
0

You have to check your default value with php value as below. For e.g.

<option value="your_default_value" <?php if(isset($filter_order_type)){ if($filter_order_type == 'your_default_value'){ echo 'selected'; } } ?> >FB Orders</option>

You have to set like this for all the option and check it with your default value by comparison with your php value.

Virb
  • 1,639
  • 1
  • 16
  • 25
0

I have used a form to submit what the user has selected. When the user submits the form the selected value will be posted and taken into the variable $entry_order_type. I think this is what you are expecting. Hope it helps. :)

<?php
$entry_order_type ="Select order type from the list and press the submit button";
if(isset($_POST['filter_order_type'])){
   $entry_order_type = $_POST['filter_order_type'];  // Storing Selected Value In Variable
}
?>
<form action="#" method="post">
<div class="form-group">
    <label class="control-label" for="input-order-type"><?php echo $entry_order_type; ?></label>

    <select name="filter_order_type" id="input-order-type" class="form-control">

      <option value=""></option>

      <option value="FB Orders" <?php if($entry_order_type=="FB Orders"){echo "selected";} ?> >FB Orders</option>
      <option value="Direct Orders" <?php if($entry_order_type=="Direct Orders"){echo "selected";} ?> >Direct Orders</option>
      <option value="Khathija's Orders" <?php if($entry_order_type=="Khathija's Orders"){echo "selected";} ?> >Khathija's Orders</option>
      <option value="Ameer's Orders" <?php if($entry_order_type=="Ameer's Orders"){echo "selected";} ?> >Ameer's Orders</option>
      <option value="Saheed's Orders" <?php if($entry_order_type=="Saheed's Orders"){echo "selected";} ?> >Saheed's Orders</option>
      <option value= "Nada's Orders" <?php if($entry_order_type=="Nada's Orders"){echo "selected";} ?> >Nada's Orders</option>
      <option value="Henath's Orders" <?php if($entry_order_type=="Henath's Orders"){echo "selected";} ?> >Henath's Orders</option>
      <option value="Noha's Orders" <?php if($entry_order_type=="Noha's Orders"){echo "selected";} ?> >Noha's Orders</option>
    </select>
    <input type="submit" name="submit" value="Get Selected Values" />

</div>
</form>
Nadun Kulatunge
  • 1,567
  • 2
  • 20
  • 28
  • your answer is almost correct but, i need after clicking the submit button. dropdown option should be as selected not empty. – Ask xah Apr 16 '18 at 10:04