Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get selected text from dropdown in PHP

I am totally new in PHP, in fact the reason I am doing this is to customize a wordpress plugin so it can fit my need. So far I have a default form, what I am doing now is to add a country dropdown. Here's how I add it

<div class="control-group">
    <label class="control-label" for="Country">Country :</label>
    <div class="controls">
        <select id="itemType_id" name="cscf[country]" class="input-xlarge">
            <option value="[email protected]">Malaysia</option>
            <option value="[email protected]">Indonesia</option> 
        </select>   
        <span class="help-inline"></span>
    </div>  
</div>

So far I only able to retrieve the value of selected item with

$cscf['country'];

How can I get the display text which is the country name in this case ?

like image 442
abiieez Avatar asked Dec 03 '22 21:12

abiieez


1 Answers

You can use a hidden field, and with JavaScript and jQuery you can set the value to this field, when the selected value of your dropdown changes.

<select id="itemType_id" name="cscf[country]" class="input-xlarge">
  <option value="[email protected]">Malaysia</option>
  <option value="[email protected]">Indonesia</option> 
</select>
<input type="hidden" name="country" id="country_hidden">

<script>
  $(document).ready(function() {
    $("#itemType_id").change(function(){
      $("#country_hidden").val(("#itemType_id").find(":selected").text());
    });
  });
</script>

Then when your page is submitted, you can get the name of the country by using

$_POST["country"]
like image 79
eyettea Avatar answered Dec 09 '22 16:12

eyettea