How to get name of selected option in dropdown?

193
May 20, 2018, at 10:10 PM

i want to get the name of selected option in dropdown.

I have a dropdown that displays course names and id from database. Now I want get the name of that course where it implement in value. but in insert query select not get name. How do i do that? Thanks

  <?php
     <select name='course' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
              foreach ($dbo->query($quer2) as $noticia2) { 
              if($noticia2['id']==@$cat){echo "<option selected                                                value='$noticia2[id]'>$noticia2[class]</option>"."<BR>";}
            else{echo  "<option value='$noticia2[id]'>$noticia2[class] </option>";}
           }
     </select>";

   if(isset($_REQUEST['sub']))
    {
  $a=$_REQUEST['course']; //in this i want to insert name not id of select option 
    }

   ?>
Answer 1

Your server get's only the data in the value property and the name of the select element passed on requests, not the Name of the options. If you want to get the class instead of the id, use the class as the value:

echo "<select name='course' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
 foreach ($dbo->query($quer2) as $noticia2) { 
   if($noticia2['id']==@$cat){
     echo "<option selected value='".$noticia2["class"]."'> ".$noticia2[class]".</option>"."<BR>";}
            else{echo  "<option value='".$noticia2["class"]."'>".$noticia2["class"] ."</option>";
    }
  }
echo "</select>";
Rent Charter Buses Company
READ ALSO
Which is the best IDE for php? [on hold]

Which is the best IDE for php? [on hold]

I’m looking for something that has its own debugger, or I can easily step through the code

134
Label name change of walker nav menu in Wordpress theme

Label name change of walker nav menu in Wordpress theme

Walker navigation menu in Wordpress does not work when I change the label name for different onesThe theme is this

167
Cannot convert string in utf8

Cannot convert string in utf8

I have this string: Deportivo La Coruña, if I return it I'll get: Deportivo la coruña, so I tried to handle the encoding in this way:

206
unable to display own error message in codeigniter

unable to display own error message in codeigniter

I am using codeigniter3now i want to delete a record then it display database following error

157