0

我想从组合框中选择一个学年,然后用它来搜索所选年份的班级字母。但我需要将选定的文本放入一个变量中。

$yeardb = "SELECT DISTINCT `year` FROM `class`"; 
        $do_year = mysqli_query($connectDB, $yeardb); 
        $num_year = mysqli_num_rows($do_year);

    Year: 
   <form method="POST" >
   <select id="yearCB" name="year" onchange="document.getElementById('selected_text').value=this.options[this.selectedIndex].text">
            <?php
                for ($i=0; $i<$num_year; $i++) 
                {
                    $yr = mysqli_fetch_array($do_year);
                    echo'<option value="'.$i.'">'.$yr ['year'].'</option>';
                }
            ?>
        </select>
        <input type="hidden" name="selected_text" id="selected_text" />
        </form>

        <?php

        if(isset($_POST['selected_text']))
        {

            $Value = $_POST['year']; 
            $make = mysql_real_escape_string($_POST['selected_text']);
        }

        $classdb = "SELECT `class` FROM `classes` WHERE `year` =".$make;
        $do_class = mysqli_query($connectDB, $classdb); 
        $num_class = mysqli_num_rows($do_class);
        ?>
4

2 回答 2

0

使用下面的代码

this.options[this.options.selectedIndex].text

 <select id="yearCB" name="year" onchange="document.getElementById('selected_text').value
                                     =this.options[this.options.selectedIndex].text">
于 2014-04-22T12:02:39.910 回答
0

在 JQuery 中

<select id="yearCB" name="year" onchange="funName(this.value)">    
</select>    
<input type="hidden" name="selected_text" id="selected_text" />    

function funName(val)    `/*function to set value*/`
{
    $("#selected_text").val(val);
}
于 2014-04-22T10:57:00.100 回答