Set variable as selection from form drop down
Posted 31 January 2006 - 10:42 PM
My aim is to have users select from a drop down box. When the selection is made an image with a href is put into the page with the href incorporating the selection from the drop down.
so far I have named the form and selection box as below
<form action="addnewmemberconfirm.php" method="post" name="userselection">
<select name="usernameselected" onChange="selecteduser()">
.....list of optons closed form ect
function selecteduser( )
var username = 'testvariable';
var usernameselected = document.userselection.usernameselected.value ;
alert ("user is " + username + usernameselected)
document.write ( '<a href="userdetails.php?username=' + usernameselected + '<img src="pic.jpg"></a>');
The alert is just for testing and comes up fine but with an empty usernameselected. I hoped the setting of the variable usernameselected would work but doesn't and I don't know how to get java to write the html exactly where the script is written.
apologies for quite how clueless I am, any help much appreciated.
Posted 02 February 2006 - 11:21 PM
to get the variable usernameselected correct change it to this:
idx = document.userselection.usernameselected.selectedIndex; var usernameselected = document.userselection.usernameselected.options[idx].value;
Put a <div id="imgplace"></div> where you want the image to show up
and then add this to the javacode, to put it there:
oImg = document.getElementById("imgplace"); oImg.innerHTML='<a href="userdetails.php?username=' + usernameselected + '<img src="pic.jpg"></a>';
Posted 03 February 2006 - 03:28 PM
Posted 05 February 2006 - 02:51 PM
Change the "selection" variable to grab the text of the options instead of the value. Or set the value param on every option in you selectbox.
var selection = document.userselection.selection.options[idx].text;
or like this:
<option value="1"> 1 <option value="2"> 2 <option value="3"> 3
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users