0

When i press the add button i need to get the value of select tag value

This is my ajax file

function insertDep(){
   var ajaxRequest;
   try{
      ajaxRequest = new XMLHttpRequest();
   }catch (e){
      try{
         ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      }catch (e) {

         try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
         }catch (e){
            alert("Your browser broke!");
            return false;
         }
      }
   }
   ajaxRequest.onreadystatechange = function(){   
      if(ajaxRequest.readyState == 4){
         var ajaxDisplay = document.getElementById('addmegp');
         ajaxDisplay.innerHTML = ajaxRequest.responseText;
      }
   }
   var unitnamedept = document.getElementById('unitnamedept').value;


   var queryString =  "?unitnamedept=" + unitnamedept;
   ajaxRequest.open("GET", "insertDept.php" + queryString, true);
   ajaxRequest.send(null); 
}

this is my html file

<select> <option name="unitnamedept" id="unitnamedept" value="0" >--</option> <option value="D01">D01</option> <option value="D02">D02</option> <option value="D03">D03</option> <option value="D04">D04</option> </select>

<input type="button" name="ang" align="right" value="Add" onclick="insertDep()"/>

** i am having another page called insertDept.php** i use this page to display

<?php
$unitnamedept = $_GET['unitnamedept'];
echo "$unitnamedept";
?>

when i pressed the button which receives only 0 value any help pls....

3 Answers3

2

Use :selected Selector to get the value of the option - jQuery

Or

Use e.options[e.selectedIndex].value
where var e = document.getElementById('SelectID') not the options

Raja Khoury
  • 3,015
  • 1
  • 20
  • 19
0
<select name="unitnamedept" id="unitnamedept"> <option  value="0" >--</option> <option value="D01">D01</option> <option value="D02">D02</option> <option value="D03">D03</option> <option value="D04">D04</option> </select>

<input type="button" name="ang" align="right" value="Add" onclick="insertDep()"/>

Change the id and name attribute to select not to the option as i had given

Sreerejith S S
  • 258
  • 5
  • 18
0

Need to place name and id attributes within select not in option @NarendraSisodia