0

I want to select a school year from a Combobox and then use it to search the classes letter from the year selected. But I need to put the selected text into a variable.

$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);
        ?>
  • You seem to be trying to do PHP actions based on live choices by the user. This isn't possible like this because PHP is performed on the server only. By the time the user's browser is showing the page, your PHP code is not there. You cannot do an SQL query of the database on your server from the user's web browser. You can do this kind of thing using Javascript and jquery since Javascript allows browser-time logic, and jquery allows you to get data from server-side actions like SQL queries. – Always Learning Apr 22 '14 at 10:16
  • Thank you, I will try with jquery now – lollipoppi Apr 22 '14 at 10:18

2 Answers2

0

In 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);
}
0

Use below code

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

 <select id="yearCB" name="year" onchange="document.getElementById('selected_text').value
                                     =this.options[this.options.selectedIndex].text">