0

This is my code i want to get the value of the selected option but how?

<?php
    $sql = "SELECT * FROM `category`";
    $category = mysql_query($sql); ?>
        <select>
            <?php
            while ($row = mysql_fetch_array($category)) {
            echo "<option id='cat' value='" . $row['catid'] ."'>" . $row['catname'] ."</option>";
            }
            ?>
        </select>
Mohsen Heydari
  • 7,256
  • 4
  • 31
  • 46
  • possible duplicate of http://stackoverflow.com/questions/1085801/how-to-get-the-selected-value-of-dropdownlist-using-javascript – TheWolf Sep 29 '13 at 16:39

2 Answers2

0

Use jQuery(http://api.jquery.com/selected-selector/), say you have:

<select name="garden" multiple="multiple">
  <option>Flowers</option>
  <option selected="selected">Shrubs</option>
  <option>Trees</option>
  <option selected="selected">Bushes</option>
  <option>Grass</option>
  <option>Dirt</option>
</select>
<div></div>

<script>
$( "select" )
  .change(function() {
    var str = "";
    $( "select option:selected" ).each(function() {
      str += $( this ).text() + " ";
    });
    $( "div" ).text( str );
  })
  .trigger( "change" );
</script>
ka_lin
  • 9,329
  • 6
  • 35
  • 56
0

Try this pure javascript solution (without jQuery)

HTML (change your current SELECT html tag by - attribute an id)

<select id="myselect">

JAVASCRIPT

var myvariable = null;
document.getElementById('myselect').onchange = function(e) {
    myvariable = this.options[this.selectedIndex].value;
}​;
Paul Rad
  • 4,820
  • 1
  • 22
  • 23