Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a value from an html select which is not in a form

Status
Not open for further replies.

shifter480

Technical User
Apr 28, 2009
12
GB
Hi,

Is it possible to get the value from an html select if it is not in a form

For example i have this code which is php.

echo '<label>Course Level: *</label>';
echo "<select name='courselevel'><option value=''>$courseLevel</option>";

$last = "";

while( $row = mysql_fetch_array( $result ) )
{
// If we have a value then dont repeat it in the list
if ( $row['level'] != $last )
{
echo '<option value="'.$row['course_id'].'" >'.$row['level'].'</option>';
}

$last = $row['coursename'];
}

echo '</select>';
echo '<br />';
echo '<br />';

echo '<label>Duration: *</label>';
echo "<select name='courseduration' onchange=\"GetAllDetails(this.value)\"><option value=''>Select Duration</option>";

for($week = 1; $week <=52; $week++)
{
echo '<option value="'.$week.'" >'.$week.'</option>';
}

echo '</select>';
echo '<br />';


my duration bit will call a function and get that value but i want it to get the level value also is this possible.

the javascript function is like so..

function GetAllDetails(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var level = level.options[level.options.selectedIndex].value;
var url="showlevels.php";
url=url+"?coursedetails="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}


any suggestions would be much appreciated

Thanks

Joe
 
Its always suggested to post the generated HTML code, rather than the server side language code (in this case PHP) that generates it.

Anyway, yes there are several ways, the easiest way would be to give the select an ID and use getElementById to get the object and then ts value.

Or using getElementsByName assuming there's only one element with the name "CourseLevel"

There are of course many other ways.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi,

Thanks,

I see what you mean but am i missing something as it is now returning this...

[object HTMLSelectElement]

function GetAllDetails(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
//var level = form.courselevel.options[form.courselevel.options.selectedIndex].value;
var level = document.getElementById('courselevel');
var url="showlevels.php";
url=url+"?coursedetails="+str + ',' + level;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

The html is

<select name='courselevel' id='courselevel'><option value=''>$courseLevel</option>

Sorry if im being stupid!

Thanks
 
By the way I have tried both

getElementsByName
getElementById

as I know that the getElementById refers to the id which is in the select.

Neither of them work
 
GetElementById returns an object, if you want the value of the select you'll need ask for it.
Code:
 var level = document.getElementById('courselevel')[red].value[/red];

That is assuming your <select has an ID of 'courselevel'

Code:
<select [red]id="courselevel"[/red] name="courselevel">



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top