Page 1 of 1

Help me to solve this

Posted: Thu Oct 30, 2008 5:23 pm
by vidyabhushan
Now about this program.... I have selected two columns from the data base. in $row[0] test_id is there, and $row[1] user_id is there. In my program my requirement is whenever i select an option from the drop down menu the corresponding $row[1] i.e user_id should be displayed on the text field, which is outside select tag.. So for that i have used javascript... but as i am new to java script i dont know much about java script.. Please help me.....

Code: Select all

 
<?
    $qry = "select test_id, user_id from QDescr_4Eval";
    $result = mysql_query($qry);
?>
 
<script type = "text/javascript">
<!--
    function send_value(form,u_id) {
        document.form.user_id.value = u_id;
    }
//-->
</script>
 

Code: Select all

 
    <h2>Tests available for Evaluation</h2>
    <td>
<?  print "<select name='test_id' id='Test' class='' tabindex='14' ><option value = ''>(Test_id), (User_id)</option>";
 
    while($row = mysql_fetch_row($result)) {
        if($t_id == $row[0] && $u_id == $row[1]) {
            ;
        }
        else {
            echo "<option value = $row[0] onselect = 'send_value(this.form, this.$row[1])'>$row[0]"; 
            
            
            $t_id = $row[0];
            $u_id = $row[1];
        }
    }
?>
    </select>
    <input type = "text" value = "" name = "user_id">
    </td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td>
    <input type = 'submit' name = 'submit' value = 'Submit'></input>
    </td>
    </tr>
    </form>
 

Re: Help me to solve this

Posted: Thu Oct 30, 2008 6:09 pm
by jackliu97
you don't really need a listener on every single options... just listen to the select actions...
your html select setup can be something like this...

Code: Select all

 
<select ... onchange="select_this_id(this)" ... >
...
</select>
//give your input an ID.
<input id="my_input" type="text>
 
and your js setup can look something like this

Code: Select all

 
function select_this_id(obj){
 //reference the input by its ID
// document.getElementByID('my_input').value = obj.value;
// edit: if you need to get the text... into the input box, 
// grab the array of options, and the index and ur set
 document.getElementByID('my_input').value =  obj.options[obj.selectedIndex].innerHTML;
}