Capture value from msdropdown dropdown box

154 Views Asked by At

In the above code I am trying to capture the value of idnum in the variable $image_result to no avail. Any help would be greatly appreciated

echo "<select name='myitem' id='myitem' action='post>";

while($row = mysql_fetch_array($result1)){
  echo "<option value='".$row["idnum"]."' data-image='/components/com_aclsfgpl/photos/p".$row["idnum"]."n1.jpg'>".$row["title"]."</option>";
}

echo "<option value='". $row['idnum']."'>".$row['title']. '</option>';
echo "</select>";
$image_result=$_POST['myitem'];
1

There are 1 best solutions below

0
On

A basic example of how you can capture the values from the dropdown, including the data attributes. The original was not valid - the select element does not have an action attribute - that belongs to the form - of which there is no sign. If you were to use this method it is trivial to then POST this data to another script/page as required or work withit in the same page with javascript.

<select name='myitem' onchange='getvalue(this)'>
    <option value='1' data-image='1'>Option 1
    <option value='2' data-image='2'>Option 2
    <option value='3' data-image='3'>Option 3
</select>

<script>
    function getvalue( n ){
        var oItem=n.options[ n.options.selectedIndex ];
        alert( oItem.value +' '+oItem.dataset.image+' '+oItem.text );
    }
</script>