I have a Dropdown that consists of two options. When I select an option the page reloads and using GET method I find out which value is selected. Then I try to make sure that the dropdown has the previously selected value and this is where things go wrong.
<?php echo $Status; ?> <!-- the value passed in the URL(stored in the variable Status) gets correctly printed here -->
<select class="btn btn-primary" required name="Status" style="float:right" onchange="location = this.value">
<option value="">Select Status</option>
<option value="?pageno=1&Status='Active'" <?php if($Status=='Active'){echo "selected";}?>>Active</option>
<option value="?pageno=1&Status='Not Active'" <?php if($Status=='Not Active'){echo "selected";}?>>Not Active</option>
</select>
The value gets printed correctly in the first line but for some reason, it isn't working in the options part. What am I doing wrong here? Can anyone clarify
CodePudding user response:
The value of your URL parameter Status
is either 'Active'
or 'Not Active'
- including those single quotes, they are part of the value that you are sending.
But what you are comparing it to, is just Active
resp. Not Active
:
if($Status=='Active')
The single quotes here are not part of the value that you are comparing $Status
to - they are part of the PHP syntax. You would have to write something like
if($Status=="'Active'")
or
if($Status=='\'Active\'')
here, to properly compare the your variable with what it actually contains.
But that makes rather little sense to begin with - you should rather remove the single quotes from the parameter value you are sending.
<option value="?pageno=1&Status=Active"
<option value="?pageno=1&Status=Not Active"
Note that I replaced the space with
here, to make this a properly URL encoded value.