Vishal
Vishal

Reputation: 13

How to save select value of dropdown to MySQl table

I have 2 drop down. In 2nd drop down items are added dynamically based on 1st drop down selection.

Now I want selected value of 5th subject to store in MySQL table. I am not able to get the value of option which is selected in 2nd drop down.

How can I get that selected value as the items are dynamically added in option tag

here is the code

<script>
function SetSubject(objLanguage) {
    var objMedia = document.getElementById("5thsub");
    objMedia.options.length = 0;
        objMedia.disabled = false;

    switch (objLanguage.value) {
    case "Humanity":
        objMedia.options.add(new Option("Hindi"));
        objMedia.options.add(new Option("IP"));

        break;
    case "Commerce":
        objMedia.options.add(new Option("IP"));
        objMedia.options.add(new Option("Other"));
        break;
    case "Sci[PCM]":
        objMedia.options.add(new Option("CS"));
        objMedia.options.add(new Option("IP"));
        break;
    case "Sci[PCB]":
        objMedia.options.add(new Option("IP"));
        objMedia.options.add(new Option("Math"));
        break;
    default:
        objMedia.options.add(new Option("select"));
        objMedia.disabled = true;
        break;
    }
}
</script>
<tr>
    <td>Stream : &nbsp;&nbsp;&nbsp;&nbsp;
        <select name="stream" id="stream" onchange="SetSubject(this)">
           <option> Select</option>
           <option value="Humanity">Humanity</option>
           <option value="Commerce">Commerce</option>
           <option value="Sci[PCM]">Sci[PCM]</option>
           <option value="Sci[PCB]">Sci[PCB]</option>
        </select>
    </td>
    <td>5th Subjects :       
        <select name="5thsub" id="5thsub" disabled="disabled">
            <option>Select</option>
        </select>
    </td>
</tr>

Part of form where data is passed to savedata.php file for saving the value.

<td>Category : &nbsp;&nbsp;&nbsp;&nbsp;<select name="cast_cate" >   
        <option value="GEN"<?php if(isset($_POST['cast_cate'])){if($_POST['cast_cate']=="GEN"){?>selected <?php }} ?>>General</option>
        <option value="SC" <?php if(isset($_POST['cast_cate'])){if($_POST['cast_cate']=="SC"){?>selected <?php }} ?>>SC</option>
        <option value="ST" <?php if(isset($_POST['cast_cate'])){if($_POST['cast_cate']=="ST"){?>selected <?php }} ?>>ST</option>
        <option value="CREM" <?php if(isset($_POST['cast_cate'])){if($_POST['cast_cate']=="CREM"){?>selected <?php }} ?>>OBC - CREAMY</option>
        <option value="NCREM" <?php if(isset($_POST['cast_cate'])){if($_POST['cast_cate']=="NCREM"){?>selected <?php }} ?>>OBC - NON-CREAMY</option>

        </select> </td>

Upvotes: 0

Views: 1497

Answers (1)

MorKadosh
MorKadosh

Reputation: 6006

You can add an "Hidden" input object that contains the value of the second select box, and write a simple function that will assign the value into that box:

add this to your js:

function setValue()
{
    document.getElementById("selected_value").value = document.getElementById("5thsub").value;
}

than change the first select box, so it will run this function:

<select name="stream" id="stream" onchange="SetSubject(this);setValue()">

last, make the second select box run this function:

<select name="5thsub" id="5thsub" disabled="disabled" onChange="setValue();">

now reading the selected value would be much easier for you.

Upvotes: 1

Related Questions