0

Hello

I have a question.

let me have this select:

<form>
<select id="mymenu" name="id" onchange="getID()">
<option></option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>								
</select>

And Now i want to get the value from mymenu, to use it i another select in the same form.
I searched in the net and i found that i should use onchange

<select name="uid">
<option></option>
<?
$sql = mysql_query("Select * from $table WHERE id = '$ch_id'");
while ($row = mysql_fetch_array($sql)) {
   ?>
   <option value="<? echo $row['unit_id']; ?>"><? echo $row['unit_name'];?></option>
   <?
}
?>
</select>
</form>

this is the getID() function:

<script type="text/javascript">
function getChapterID(){ //run some code when "onchange" event fires
var selectmenu=document.getElementById("mymenu")
<?$ch_id?> = this.options[this.selectedIndex].value
}
</script>
3
Contributors
4
Replies
7
Views
5 Years
Discussion Span
Last Post by diafol
0

How exactly do you want to use the value from selectbox "mymenu" in selectbox "uid". It seems you pretty much have the idea, except in your getChapterID() you should be retrieving the value of "selectmenu" rather than of "this.options"

0

this is the getID() function:

No it's not,

function getChapterID(){ //run some code when "onchange" event fires

will not run when you:

<select id="mymenu" name="id" onchange="getID()">

getID() is not the same as getChapterID()

0

Sorry

I just make a mistake

but it's still don't work

I did this changes:

<form>
    <select id="mymenu" name="id" onchange="getID()">
    <option></option>
    <option value="1">A</option>
    <option value="2">B</option>
    <option value="3">C</option>
    </select>
<select name="uid">
    <option></option>
    <?
    $sql = mysql_query("Select * from $table WHERE id = '$ch_id'");
    while ($row = mysql_fetch_array($sql)) {
    ?>
    <option value="<? echo $row['unit_id']; ?>"><? echo $row['unit_name'];?></option>
    <?
    }
    ?>
    </select>
    </form>
<script type="text/javascript">
    function getID(){ //run some code when "onchange" event fires
    var selectmenu=document.getElementById("mymenu")
    <?$ch_id?> = selectmenu[this.selectedIndex].value
    }
    </script>
0

Avoid short tags if possible.

I can't see how this works.

<?$ch_id?> = selectmenu[this.selectedIndex].value

You're mixing php and js. JS can't make a call to php as JS works on the client and PHP works on the server. This can only be achieved through Ajax.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.