How to pre select an option from dropdown menu in PHP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm working on a WordPress theme for a listing directory, and I'm trying to pre select an option from a dropdown menu so that when someone opens that page they will get the listings from that option right away.
I tried with jQuery but it didn't work so I'm stuck here right now. Any help is welcome.
Image: https://imgur.com/a/G0Rtny3
EDIT: I found a fix, I made "Producer" the only option, and changed the if statement from !empty($term_ID) to empty($term_ID).
It might not be the best solution but it works for me. Thanks everyone.
<select data-placeholder="<?php echo esc_html__('Select Category','dwt-
listing');?>" name="l_category" class="allow_clear" id="l_category">
<option value=""><?php echo esc_html__('Select an
option','dwt-listing'); ?>
</option>
<option value="all"><?php echo esc_html__('All Categories','dwt-
listing'); ?>
<?php
//selective
if( isset( $_GET['l_category'] ) && $_GET['l_category'] != "" )
{
$term_ID = $_GET['l_category'];
}
foreach( $listing_cats as $cats )
{
?>
<option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>
<?php
}
?>
</select>
php wordpress directory-listing
add a comment |
I'm working on a WordPress theme for a listing directory, and I'm trying to pre select an option from a dropdown menu so that when someone opens that page they will get the listings from that option right away.
I tried with jQuery but it didn't work so I'm stuck here right now. Any help is welcome.
Image: https://imgur.com/a/G0Rtny3
EDIT: I found a fix, I made "Producer" the only option, and changed the if statement from !empty($term_ID) to empty($term_ID).
It might not be the best solution but it works for me. Thanks everyone.
<select data-placeholder="<?php echo esc_html__('Select Category','dwt-
listing');?>" name="l_category" class="allow_clear" id="l_category">
<option value=""><?php echo esc_html__('Select an
option','dwt-listing'); ?>
</option>
<option value="all"><?php echo esc_html__('All Categories','dwt-
listing'); ?>
<?php
//selective
if( isset( $_GET['l_category'] ) && $_GET['l_category'] != "" )
{
$term_ID = $_GET['l_category'];
}
foreach( $listing_cats as $cats )
{
?>
<option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>
<?php
}
?>
</select>
php wordpress directory-listing
Does this code set the correct option as selected in the dropdown?
– RiggsFolly
Jan 3 at 10:03
This code is for the default placeholder "Select Category". I'm trying to set the "Producer" option as default.
– lordotG
Jan 3 at 10:47
add a comment |
I'm working on a WordPress theme for a listing directory, and I'm trying to pre select an option from a dropdown menu so that when someone opens that page they will get the listings from that option right away.
I tried with jQuery but it didn't work so I'm stuck here right now. Any help is welcome.
Image: https://imgur.com/a/G0Rtny3
EDIT: I found a fix, I made "Producer" the only option, and changed the if statement from !empty($term_ID) to empty($term_ID).
It might not be the best solution but it works for me. Thanks everyone.
<select data-placeholder="<?php echo esc_html__('Select Category','dwt-
listing');?>" name="l_category" class="allow_clear" id="l_category">
<option value=""><?php echo esc_html__('Select an
option','dwt-listing'); ?>
</option>
<option value="all"><?php echo esc_html__('All Categories','dwt-
listing'); ?>
<?php
//selective
if( isset( $_GET['l_category'] ) && $_GET['l_category'] != "" )
{
$term_ID = $_GET['l_category'];
}
foreach( $listing_cats as $cats )
{
?>
<option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>
<?php
}
?>
</select>
php wordpress directory-listing
I'm working on a WordPress theme for a listing directory, and I'm trying to pre select an option from a dropdown menu so that when someone opens that page they will get the listings from that option right away.
I tried with jQuery but it didn't work so I'm stuck here right now. Any help is welcome.
Image: https://imgur.com/a/G0Rtny3
EDIT: I found a fix, I made "Producer" the only option, and changed the if statement from !empty($term_ID) to empty($term_ID).
It might not be the best solution but it works for me. Thanks everyone.
<select data-placeholder="<?php echo esc_html__('Select Category','dwt-
listing');?>" name="l_category" class="allow_clear" id="l_category">
<option value=""><?php echo esc_html__('Select an
option','dwt-listing'); ?>
</option>
<option value="all"><?php echo esc_html__('All Categories','dwt-
listing'); ?>
<?php
//selective
if( isset( $_GET['l_category'] ) && $_GET['l_category'] != "" )
{
$term_ID = $_GET['l_category'];
}
foreach( $listing_cats as $cats )
{
?>
<option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>
<?php
}
?>
</select>
php wordpress directory-listing
php wordpress directory-listing
edited Jan 3 at 13:27
lordotG
asked Jan 3 at 10:00
lordotGlordotG
11
11
Does this code set the correct option as selected in the dropdown?
– RiggsFolly
Jan 3 at 10:03
This code is for the default placeholder "Select Category". I'm trying to set the "Producer" option as default.
– lordotG
Jan 3 at 10:47
add a comment |
Does this code set the correct option as selected in the dropdown?
– RiggsFolly
Jan 3 at 10:03
This code is for the default placeholder "Select Category". I'm trying to set the "Producer" option as default.
– lordotG
Jan 3 at 10:47
Does this code set the correct option as selected in the dropdown?
– RiggsFolly
Jan 3 at 10:03
Does this code set the correct option as selected in the dropdown?
– RiggsFolly
Jan 3 at 10:03
This code is for the default placeholder "Select Category". I'm trying to set the "Producer" option as default.
– lordotG
Jan 3 at 10:47
This code is for the default placeholder "Select Category". I'm trying to set the "Producer" option as default.
– lordotG
Jan 3 at 10:47
add a comment |
2 Answers
2
active
oldest
votes
This example could help
var select = document.getElementById('countryselect');
var option;
for (var i=0, i<select.options.length; i<iLen; i++) {
option = select.options[i];
if (option.value == '4') {
// or
// if (option.text = 'Malaysia') {
option.setAttribute('selected', true);
// For a single select, the job's done
return;
}
}
Here is the link
How to add "selected" in option attribute using Javascript or jQuery?
add a comment |
I have made a little change in your code.
check this and first in sure that you have got $term_ID or not because your selected option depend on this variable.
Replace this:-
<option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>
With this:-
<option value="<?php echo esc_attr( $cats->term_id ); ?>" <?php if ($cats->term_id==$term_ID) { echo "selected='selected'"; } ?> ><?php echo esc_html( $cats->name ); ?></option>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019975%2fhow-to-pre-select-an-option-from-dropdown-menu-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This example could help
var select = document.getElementById('countryselect');
var option;
for (var i=0, i<select.options.length; i<iLen; i++) {
option = select.options[i];
if (option.value == '4') {
// or
// if (option.text = 'Malaysia') {
option.setAttribute('selected', true);
// For a single select, the job's done
return;
}
}
Here is the link
How to add "selected" in option attribute using Javascript or jQuery?
add a comment |
This example could help
var select = document.getElementById('countryselect');
var option;
for (var i=0, i<select.options.length; i<iLen; i++) {
option = select.options[i];
if (option.value == '4') {
// or
// if (option.text = 'Malaysia') {
option.setAttribute('selected', true);
// For a single select, the job's done
return;
}
}
Here is the link
How to add "selected" in option attribute using Javascript or jQuery?
add a comment |
This example could help
var select = document.getElementById('countryselect');
var option;
for (var i=0, i<select.options.length; i<iLen; i++) {
option = select.options[i];
if (option.value == '4') {
// or
// if (option.text = 'Malaysia') {
option.setAttribute('selected', true);
// For a single select, the job's done
return;
}
}
Here is the link
How to add "selected" in option attribute using Javascript or jQuery?
This example could help
var select = document.getElementById('countryselect');
var option;
for (var i=0, i<select.options.length; i<iLen; i++) {
option = select.options[i];
if (option.value == '4') {
// or
// if (option.text = 'Malaysia') {
option.setAttribute('selected', true);
// For a single select, the job's done
return;
}
}
Here is the link
How to add "selected" in option attribute using Javascript or jQuery?
answered Jan 3 at 10:08
TDKTDK
961720
961720
add a comment |
add a comment |
I have made a little change in your code.
check this and first in sure that you have got $term_ID or not because your selected option depend on this variable.
Replace this:-
<option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>
With this:-
<option value="<?php echo esc_attr( $cats->term_id ); ?>" <?php if ($cats->term_id==$term_ID) { echo "selected='selected'"; } ?> ><?php echo esc_html( $cats->name ); ?></option>
add a comment |
I have made a little change in your code.
check this and first in sure that you have got $term_ID or not because your selected option depend on this variable.
Replace this:-
<option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>
With this:-
<option value="<?php echo esc_attr( $cats->term_id ); ?>" <?php if ($cats->term_id==$term_ID) { echo "selected='selected'"; } ?> ><?php echo esc_html( $cats->name ); ?></option>
add a comment |
I have made a little change in your code.
check this and first in sure that you have got $term_ID or not because your selected option depend on this variable.
Replace this:-
<option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>
With this:-
<option value="<?php echo esc_attr( $cats->term_id ); ?>" <?php if ($cats->term_id==$term_ID) { echo "selected='selected'"; } ?> ><?php echo esc_html( $cats->name ); ?></option>
I have made a little change in your code.
check this and first in sure that you have got $term_ID or not because your selected option depend on this variable.
Replace this:-
<option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>
With this:-
<option value="<?php echo esc_attr( $cats->term_id ); ?>" <?php if ($cats->term_id==$term_ID) { echo "selected='selected'"; } ?> ><?php echo esc_html( $cats->name ); ?></option>
answered Jan 3 at 10:58
F5 BuddyF5 Buddy
3584
3584
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019975%2fhow-to-pre-select-an-option-from-dropdown-menu-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does this code set the correct option as selected in the dropdown?
– RiggsFolly
Jan 3 at 10:03
This code is for the default placeholder "Select Category". I'm trying to set the "Producer" option as default.
– lordotG
Jan 3 at 10:47