Why the selected dropdown value is not getting shown? - php

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>

Related

Selectize is removing optgroup options

I am using selectize (a select2.js alternative). When I am running selectize on my field with 2 optgroups, one of the optgroups disappears \(〇_o)/
This is the HTML of the select field:
This is how it looks in view source:
It seems like the quotation symbol is causing it trouble because if I remove that value from the array before printing the values, the optgroup that disappears now appears. I do need however to have these symbols.
This is the PHP code that generates that HTML:
<select name="output_selected_columns[]" id="output_selected_columns" class="selectize" multiple="multiple">
<optgroup label="<?php echo sprintf( __( 'Columns of %s', 'excellico' ), $file1 ); ?>">
<?php
foreach( $Excellico_Files->get_file_left_header() as $key => $value ) {
?>
<option value="<?php echo esc_html( $value ); ?>"><?php echo esc_html( $value ); ?></option>
<?php
}
?>
</optgroup>
<optgroup label="<?php echo sprintf( __( 'Columns of %s', 'excellico' ), $file2 ); ?>">
<?php
foreach( $Excellico_Files->get_file_right_header() as $key => $value ) {
?>
<option value="<?php echo esc_html( $value ); ?>"><?php echo esc_html( $value ); ?></option>
<?php
}
?>
</optgroup>
</select>
This is how it looks: (missing File 1 optgroup)
I also tried to add diacritics: true to the selectize options object, didn't help.
Any help is appreciated!
Thanks!
What should I do?
The html output you provided is correct as can be seen.
<select name="output_selected_columns[]" id="output_selected_columns" class="selectize" multiple="multiple">
<optgroup label="File 1">
<option value="מוצר">מוצר</option>
<option value="הזמנה">הזמנה</option>
<option value="ת"ז">ת"ז</option>
<option value="שם מנוי">שם מנוי</option>
</optgroup>
<optgroup label="File 2">
<option value="מוצר">מוצר</option>
<option value="הזמנה">הזמנה</option>
<option value="ת"ז">ת"ז</option>
<option value="שם מנוי">שם מנוי</option>
<option value="סטטוס תגמול">סטטוס תגמול</option>
</optgroup>
</select>
However it doesn't look it is generated by the php code you provided. Since that contains ' and your html contains "
Try any of the following hopefully it will work
<option value="<?php echo esc_html( $value ); ?>"><?php echo esc_html( $value ); ?></option>
<option value="<?php echo esc_attr( $value ); ?>"><?php echo esc_html( $value ); ?></option>

How to pre select an option from dropdown menu in 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>

How to update a dropdown value in codeigniter

This i my View code:
<form role="form" action="<?php echo base_url() ?>add_customer" method="post">
<select class="form-control" id="customer_id" name="customer_id">
<?php foreach ( $customer as $cust ){?>
<option value="<?php echo $datas[0]->customer_id; ?>"<?php if($cust->customer_id==$datas[0]->customer_id) echo 'selected="selected"'; ?>> <?php echo $cust->customer_id; ?></option>
<?php }?>
</select>
</form>
For Eg: Dropdown Value contains 1,2,3,4 actually selected value is 2 display in that field correctly. Now, I want to update the dropdown value to 4
How can I do this? Please help..
Hope this will help you :
Change values from value="<?php echo $datas[0]->customer_id; ?>" to this value="<?=$cust->customer_id;?>"
<select class="form-control" id="customer_id" name="customer_id">
<?php foreach ( $customer as $cust ){?>
<option value="<?=$cust->customer_id;?>" <?php if($cust->customer_id == $datas[0]->customer_id) echo 'selected="selected"'; ?> > <?php echo $cust->customer_id; ?></option>
<?php }?>
</select>

PHP Adding an option to sort by date

I have a listing site in wordpress where the irems are automatically sorted by date posted (newest first) when you go to the search results page. There are already options to sort by price (high to low, low to high) but I ned to add the option to sort by date (newest first, oldest first)
The developer of theme told me the files i need to modify but wouldn't give me any help other than that.
I decided to try duplicating the code for sort by price and changing "price" to "date"
Here is the code i added into "Search-listings.php"
/*———————————————————————————–*/
/* Sort By date posted */
/*———————————————————————————–*/
function sort_by_date() {
$extraVars = “”;
foreach($_GET AS $key=>$value) {
$extraVars .= ”;
} ?>
<form action="” name=”order “class=”formsrch” method=”get”>
<option value="DESC" selected=”selected” >
<option value="ASC" selected=”selected” >
<?php }
and here is the code i added to the "theme-functions.php"
/*-----------------------------------------------------------------------------------*/
/* Sort By date posted */
/*-----------------------------------------------------------------------------------*/
function sort_by_date() {
$extraVars = "";
foreach($_GET AS $key=>$value) {
$extraVars .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
} ?>
<form action="<?php get_site_url(); ?>" name="order "class="formsrch" method="get">
<?php echo $extraVars;?>
<select class="ct_orderby_price" id="ct_orderby_date" name="ct_orderby_date">
<option value=""><?php _e('Sort By', 'contempo'); ?></option>
<option value="DESC" <?php if($_GET['ct_orderby_date'] == 'DESC'){ ?> selected="selected" <?php } ?>><?php _e('Date - Newest', 'contempo'); ?></option>
<option value="ASC" <?php if($_GET['ct_orderby_date'] == 'ASC'){ ?> selected="selected" <?php } ?>><?php _e('Date - Oldest', 'contempo'); ?></option>
</select>
</form>
<?php }
Unsurprisingly that didn't work, I'm a beginner at php so any advice
would be appreciated
OK, so i've figured out i need to add the code to the existing function like so
/*-----------------------------------------------------------------------------------*/
/* Sort By Price */
/*-----------------------------------------------------------------------------------*/
function sort_by_price() {
$extraVars = "";
foreach($_GET AS $key=>$value) {
$extraVars .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
} ?>
<form action="<?php get_site_url(); ?>" name="order "class="formsrch" method="get">
<?php echo $extraVars;?>
<select class="ct_orderby_price" id="ct_orderby_price" name="ct_orderby_price">
<option value=""><?php _e('Sort By', 'contempo'); ?></option>
<option value="DESC" <?php if($_GET['ct_orderby_price'] == 'DESC'){ ?> selected="selected" <?php } ?>><?php _e('Price - Highest', 'contempo'); ?></option>
<option value="ASC" <?php if($_GET['ct_orderby_price'] == 'ASC'){ ?> selected="selected" <?php } ?>><?php _e('Price - Lowest', 'contempo'); ?></option>
<option value="" <?php if($_GET['the_date'] == 'ASC'){ ?> selected="selected" <?php } ?>><?php _e('Date - Newest First', 'contempo'); ?></option>
</select>
</select>
</form>
<?php }
I still can't make the "order by date" work as i don't know what to put in in place of "ct_orderby_price" and if I use the ASC or the DESC option values it just jumps to the price options?

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>

Categories