Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass selected option value from select to php

    <style type="text/css">
    form select {
        display:none;
    }
    form select.active {
        display:block;
    }
    </style>
    <script type="text/javascript">
    window.onload = function () {
        var allElem = document.getElementsByName("sl");
        for (i = 0; i < allElem.length; i++) {
            allElem[i].addEventListener('change', toggleDisplay);
        }
   }
    function toggleDisplay(e) {
       var id = 's' + this.value;
        var currentSelect = document.getElementsByClassName("active")[0];
        if (currentSelect) {
            currentSelect.className = "";
        }
        document.getElementById(id).className = "active";
    }    
    </script>
    </head><body>
    <form action="check_a.php">
    <h2><b>Source: </b></h2>
    <input type="radio" name="sl"  value="c">Central</input>
    <input type="radio" name="sl"  value="h">Eastern</input>
    <input type="radio" name="sl" value="w">Western</input>

    <select id="sc" name="sc">
    <option value="1">1</option>
    <option value="2">2</option>
    </select>

    <select id="sh">
    <option value="3">3</option>
    <option value="4">4</option>
    </select>

    <select id="sw">
    <option value="5">5</option>
    <option value="6">6</option>
    </select>
    <input type="submit" name="submit" value="submit"/>

    </form></body></html>

there is one radio button whose value displays the specific select box, which is done using javascript. the value of tag needs to be passed to php code. How can i pass the selected option value to php?

like image 864
Sanjay Malhotra Avatar asked Jul 04 '13 08:07

Sanjay Malhotra


People also ask

How to pass selected value of dropdown in PHP?

Summary. Use the <select> element to create a dropdown list. Use the multiple attribute to create a list that allows multiple selections. Use $_POST to get the selected value of the select element if the form method is POST (or $_GET if the form method is GET ).

How to POST selected radio button value in PHP?

To display radio buttons value. <? php if (isset($_POST['submit'])) { if(isset($_POST['radio'])) { echo "<span>You have selected :<b> ". $_POST['radio']."</b></span>"; } else{ echo "<span>Please choose any radio button.

How do you get the selected value of dropdown in PHP without submit?

How do you get the selected value of a dropdown in PHP without submitting? You have to use JavaScript for this, PHP is server side language so it will not able to get drop down value without form submitting. by using JavaScript you can get it .


1 Answers

Can't you just set all the select elements to have the same name?

<select id="sc" name="tagvalue">
    <option value="1">1</option>
    <option value="2">2</option>
</select>

<select id="sh" name='tagvalue'>
    <option value="3">3</option>
    <option value="4">4</option>
</select>

<select id="sw" name='tagvalue'>
    <option value="5">5</option>
    <option value="6">6</option>
</select>

Then, just take the value of $_POST['tagvalue'].

like image 128
Ben Avatar answered Oct 04 '22 22:10

Ben