Reload page when dropdown changes and pass this value

I have a dropdown on my page. When I select an option from the dropdown the page should reload and return to the same page and the value of this dropdown should be passed
Below is the php code for the dropdown

<select style="width: 200px;" name="location" onchange="window.location='index.php?id=' + this.value;">
    <option value="All">All</option>
    <option value="Noida Sector 1">Noida Sector 1</option>
    <option value="Noida Sector 2">Noida Sector 2</option>
    <option value="Noida Sector 3">Noida Sector 3</option>
    <option value="Noida Sector 4">Noida Sector 4</option>
    <option value="Noida Sector 5">Noida Sector 5</option>
    <option value="Noida Sector 6">Noida Sector 6</option>
    <option value="Noida Sector 7">Noida Sector 7</option>
</select>

<?php $location=$_POST['id']; ?>

      

I am trying to pass a value to $ location.

+3


source to share


4 answers


Just change your php code, this code will select the select option and highlight the selected value (shown below)



    <select style = "width: 200px;" id = "myselect" name = "location" onchange = "window.location = 'index.php? id =' + this.value + '& pos =' + this.selectedIndex;">
        <option value = "All"> All </option>
        <option value = "Noida Sector 1"> Noida Sector 1 </option>
        <option value = "Noida Sector 2"> Noida Sector 2 </option>
        <option value = "Noida Sector 3"> Noida Sector 3 </option>
        <option value = "Noida Sector 4"> Noida Sector 4 </option>
        <option value = "Noida Sector 5"> Noida Sector 5 </option>
        <option value = "Noida Sector 6"> Noida Sector 6 </option>
        <option value = "Noida Sector 7"> Noida Sector 7 </option>
    </select>

    <? php
    if (isset ($ _ GET ['id']))
    {
        $ location = $ _ GET ['id'];
        echo $ location;
    ?>
    <script>
        var myselect = document.getElementById ("myselect");
        myselect.options.selectedIndex = <? php echo $ _GET ["pos"]; ?>
    </script>
    <? php
    }
    ?>

+3


source


Is it possible for you to select in the form (action = POST)? You can do this (using jquery):



$('select').on('change', function () {
    $(this).parent().submit();
});

      

0


source


Use below code,

<?php if (isset($_GET['id'])) { echo $_GET['id']; } ?>

0


source


@Prabhjot Singh kainth, use like this code,

<option value="Noida Sector 3" <?php if ($_GET['id']=='Noida Sector 3'): ?>
selected=selected   
<?php endif ?>>Noida Sector 3</option>

      

0


source







All Articles