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>

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"

Member Avatar for diafol

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()

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>
Member Avatar for diafol

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.