Reputation: 1861
I have a dropdown in my page.When I select an option from dropdown the page should reload and come back to same page and the value of that dropdown should be passed
Below is the php code for 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 the value to $location.
Upvotes: 3
Views: 7371
Reputation: 1814
Just change your php code ,this code outputs selection option and highlights 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 } ?>
Upvotes: 5
Reputation: 604
@ 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>
Upvotes: 0
Reputation: 604
Use below code,
<?php if (isset($_GET['id'])) {
echo $_GET['id'];
} ?>
Upvotes: 0
Reputation: 110
Is it an option for you to put the select in a form (action=POST)? The you could do this (using jquery):
$('select').on('change', function () {
$(this).parent().submit();
});
Upvotes: 0