How to pre select an option from dropdown menu in PHP - php

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>

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?

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>

Related

Why the selected dropdown value is not getting shown?

I am building a function on my WordPress plugin where it displays a dropdown of all the available pages. When I click on Save Changes, the value gets saved perfectly in the DB. It updates the value perfectly too. But, the selected value is not being shown in the dropdown. When Save Changes is clicked the value gets saved, but the dropdown again resets to "Choose one". It doesn't get to display the selected option. Am I doing something wrong here? Any guidance is appreciated.
<form method=post>
<div class="header-right">
<?php
$posts = get_pages(
array(
'post_status' => 'publish',
)
);
?>
<select name="page_for_logged_in" id="page_for_logged_in">
<option selected="selected">Choose one</option>
<?php
foreach ( $posts as $page ) {
?>
<option value="<?php echo esc_attr( $page->post_name ); ?>" <?php selected(get_option('page_for_logged_in'), 'page')?>><?php echo esc_html( $page->post_title ); ?></option>
<?php
}
?>
</select>
<?php
if(empty($_POST['page_for_logged_in'])) {
} else {
$myvalue=$_POST['page_for_logged_in'];
update_option('page_for_logged_in', $myvalue, $autoload = 'no');
}
?>
<?php submit_button(); ?>
</p>
</br>
</br>
</form>
Okay, so I figured out the working solution to my issue. Pasted below; Might be helpful for someone.
<form method=post>
<div class="header-right">
<?php
$posts = get_pages(
array(
'post_status' => 'publish',
)
);
?>
<?php
if(empty($_POST['page_for_logged_in'])) {
} else {
$myvalue=$_POST['page_for_logged_in'];
update_option('page_for_logged_in', $myvalue, $autoload = 'yes');
}
?>
<select name="page_for_logged_in" id="page_for_logged_in">
<option value="" disabled selected>Choose one</option>
<?php
foreach ( $posts as $page ) {
?>
<option value="<?php echo esc_attr( $page->post_title ); ?>" <?php echo ( get_option('page_for_logged_in') == $page->post_title ? 'selected' : '' ); ?>><?php echo esc_html( $page->post_title ); ?></option>
<?php
}
?>
</select>
<?php submit_button(); ?>
</p>
</br>
</br>
</form>

Select option value

I have a selection dropdown. Now what i want to accomplish is having a default first value (insted of an empty line in the pulldown menu).
The value needs to be "All manufacturers" and should be displayed always (like a placeholder.
If nothing is chosen, it will automatically search all the products of all manufacturers
<option <?php if(!isset($brand)) { echo 'selected="yes"' ; } ?> ></option>
<?php usort(
$manufacturers,
function($a, $b) {
return strcmp($a['name'], $b['name']);
}
)
?>
<?php foreach ($manufacturers as $manufacturer) { ?>
<?php if ($manufacturer['manufacturer_id'] == $manufacturer_id) { ?>
<option value="<?php echo $manufacturer['manufacturer_id']; ?>" selected="selected"><?php echo $manufacturer['name']; ?></option>
<?php } else { ?>
<option value="<?php echo $manufacturer['manufacturer_id']; ?>"><?php echo $manufacturer['name']; ?></option>
<?php } ?>
<?php } ?>
</select>
Any help appreciated!
If I understand you correctly I think you want to do this
<option value"0" <?php if(!isset($brand)) { echo 'selected="yes"' ; } ?> >All Manufacturers</option>

PHP drop down list from db, view selected post and UPDATE post

Hi I would like to have it to work with two fields, both id and text but I cant even get this to work.
I get a post from the database to update.
If I have a ordinary optionlist. It views the selected value and I can change option and save it to the database.
This works:
<select name="ddlLinkType" title="<?php echo $row_visa_link['LinkType']; ?>">
<option value="" <?php if (!empty($row_visa_link['LinkType']) && $row_visa_link['LinkType'] == '' ) echo 'selected = "selected"'; ?>></option>
<option value="Styrelsen" <?php if (!empty($row_visa_link['LinkType']) && $row_visa_link['LinkType'] == 'Styrelsen') echo 'selected = "selected"'; ?>>Styrelsen</option>
<option value="Alla" <?php if (!empty($row_visa_link['LinkType']) && $row_visa_link['LinkType'] == 'Alla') echo 'selected = "selected"'; ?>>Alla</option>
<option value="Option3" <?php if (!empty($row_visa_link['LinkType']) && $row_visa_link['LinkType'] == 'Option3') echo 'selected = "selected"'; ?>>Option3</option>
</select>
But I would like to get the options dynamic from database table. I have tried many things now but nothing works. I am so confused and everything is messed up right now. I am trying to generate the optionlist without loosing the function that it shows the selected value from the post to be updated.
This doesnt work:
<select name="ddlLinkCategory" title="<?php echo $row['LinkCatID']; ?>">
<?php while($row = mysqli_fetch_assoc($resultCategoy)) { ?>
<!-- Dont know how to solve this-->
<option value="<?php($row['LinkCatID'])?>" <?php if (!empty($row_visa_link['LinkCategory']) && $row_visa_link['LinkCategory'] == $row['LinkCatID']) echo 'selected = "selected"'; ?>>$row['LinkCatID']</option>
<?php }} ?>
</select>
Thank You.
This code is wrong:
<select name="ddlLinkCategory" title="<?php echo $row['LinkCatID']; ?>">
<?php while($row = mysqli_fetch_assoc($resultCategoy)) { ?>
You dont acces to <?php echo $row['LinkCatID']; ?> in line 1, $row is defined in line 2.
Thank You! Now it Works :)
<select name="ddlLinkCategory" title="<?php echo $row_visa_link['LinkCategory']; ?>">
<?php while($row = mysqli_fetch_assoc($resultCategory)) { ?>
<option value= "<?php echo($row['LinkCatID'])?>"<?php if (!empty($row_visa_link['LinkCategory']) && $row_visa_link['LinkCategory'] == $row['LinkCatID']) echo 'selected = "selected"'; ?>> <?php echo $row['LinkCatID']?></option>
<?php }} ?>
</select>

Adding an "all" option on categories

This may seem like a basic question but I hope somebody could help me out.
I have a created a widget for a custom post type so that I could show the post types in a dynamic sidebar. In the widget, I have also created a dropdown that gets the categories to show.
My problem is that I also want to have the option to show all the categories and not just 1 category for the dropdown. Here's the code I have so far:
<select id="<?php echo $this->get_field_id('articleCategory'); ?>" name="<?php echo $this->get_field_name('articleCategory'); ?>">
<?php $arr = get_categories(); ?>
<?php foreach($arr as $option) { ?>
<option <?php echo $instance['articleCategory'] == $option->term_id ? 'selected="selected"' : '';?> value="<?php echo $option->term_id; ?>"><?php echo $option->name; ?></option>
<?php } ?>
</select>
Should I just use a multi-select for this and how do i go about it?
this will echo out all your categories but you'll have to update the selected part to match your instance.
<option value="<?php foreach($arr as $option) {echo $option->term_id.','; };?>"><?php echo $arr; ?>All Categories</option>
with your full code
<select id="<?php echo $this->get_field_id('articleCategory'); ?>" name="<?php echo $this->get_field_name('articleCategory'); ?>">
<?php $arr = get_categories(); ?>
<option value="<?php foreach($arr as $option) {echo $option->term_id.','; };?>"><?php echo $arr; ?>All Categories</option>
<?php foreach($arr as $option) { ?>
<option <?php echo $instance['articleCategory'] == $option->term_id ? 'selected="selected"' : '';?> value="<?php echo $option->term_id; ?>"><?php echo $option->name; ?></option>
<?php } ?>
</select>

Highlighting multiple selections on a form after submitting

This select box below does remember and highlight -one- selection after submitting the form. But when i make it multiple, it doesn't highlight any of the selectionS after submitting.
Any idea about how to achieve this?
Thanks in advance.
<?php
$options_amount = array("0","1","2","3","4","5","6","7","8","9","10+");
$no_way = $_GET['no_way'];
?>
<select class="postform" name="no_way[]" multiple size="5">
<option <?php if ($no_way == 'all') { ?>selected="selected"<?php }?> value="all">Any</option>
<?php
foreach ($options_amount as $option) {
?><option <?php if ($no_way == $option) { ?>selected="selected"<?php }?> value="<?php echo $option; ?>"><?php echo $option; ?></option><?php }?>
</select>
$_GET['no_way'] only handles single parameters you have to use $_GET['no_way[]'] and in_array($option, $no_way)
This works for me:
<?php
$options_amount = array("0","1","2","3","4","5","6","7","8","9","10+");
$no_way = $_GET['no_way'];
?>
<select class="postform" name="no_way[]" multiple size="5">
<option <?php if (in_array("all",$no_way)) { ?>selected="selected"<?php }?> value="all">Any</option>
<?php
foreach ($options_amount as $option) {
?><option <?php if (in_array($option,$no_way)) { ?>selected="selected"<?php }?> value="<?php echo $option; ?>"><?php echo $option; ?></option><?php }?>
</select>
I'm not sure if this will help or not, but is there any chance you've tried just using selected instead of selected="selected"?
<option <?php if ($no_way == $option) { ?> selected<?php }?> value="<?php echo $option; ?>"><?php echo $option; ?></option>

Categories