Selectize is removing optgroup options - php

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>

Related

PHP: html select in form doesn't display value from array with for each

I have this array with a list of countries.
$country_list = array(
"Afghanistan",
"Albania",
"Algeria",
"Andorra",
"Angola"
};
I show these countries with a select in my form
<select name="countries">
<option value=""></option>
<?php foreach($country_list as $key => $value): ?>
<option value="<?php $key ?>"><?php $value ?></option>
<?php endforeach ?>
</select><br>
For some reason the value is showing up as an empty string in my select.
You forgot to write echo
<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
If you use php, use "echo" to display a value:
<select name="countries">
<option value=""></option>
<?php foreach($country_list as $key => $value): ?>
<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
<?php endforeach ?>
</select><br>

Pre populating selectbox using php

I have an issue for which I am not able to think of a solution.
I have an array of data which I get from the server. I have to pre populate a form which already have some values.
<select id="select1" name="Salutation" class="field-size-top-large" >
<option value="">-- please select -- </option>
<option value="Mr.">Mr.</option>
<option value="Ms.">Ms.</option>
</select>
I am getting an array
array(11) {
[0]=> string(4) "Ms."
[1]=> string(8) "Y"
}
I have to check if [0]=>'Ms.' is there in select list, if there it should be selected else default is selected. Any ideas?
use this
$options = array('Mr.', 'Ms.');
$data = array(0 => 'Ms.', 1 => 'Y');
<select id="select1" name="Salutation" class="field-size-top-large">
<option value="">-- please select --</option>
<?php foreach ($options as $option): ?>
<option value="<?php echo $option?>" <?php echo $option == $data[0] ? 'selected' : ''; ?>><?php echo $option; ?></option>
<?php endforeach; ?>
</select>
Try this:
$options = ['Mr.', 'Ms.']; // the default options
$data = [0 => 'Ms.', 1 => 'Y']; // data from server
<select id="select1" name="Salutation" class="field-size-top-large">
<option value="">-- please select --</option>
<?php foreach ($options as $option): ?>
<option value="<?php echo $option; ?>" <?php echo in_array($option, $data) ? 'selected' : ''; ?>><?php echo $option; ?></option>
<?php endforeach; ?>
</select>
My soultion:
the data array I am getting from the server i.e 0=>'Ms.' is the only value I can check for the rest are different fields
<select id="select1" name="Salutation" class="field-size-top-large" >
<option value="">-- please select --</option>
<option value="Mr." <?php echo 'Mr.'== $data[0]? 'selected':''?>> Mr. </option>
<option value="Ms."<?php echo 'Ms.'== $data[0]? 'selected':''?>>Ms.</option>
</select>

Trying to add two values on the same line using a php loop in a HTML form.

I am trying to display two seperate items on the same line in a HTML form using a PHP loop to auto populate it. This is the PHP/HTML form:
<select class="form-control" name="list" title="pick a type">
<?php if(count($postOpts)) { ?>
<?php foreach($postOpts as $row) { ?>
<option value="<?= $row["name"] ?>"><?= $row["price"] ?></option>
<?php } ?>
<?php } else { ?>
<option value="<?= $post->getDefaultPrice() ?>"><?= "Default Shipping Option - $".$post->getDefaultPrice() ?></option>
<?php } ?>
</select>
I can have either the price or the name displaying, but not both on the same line.
Can anyone help?
May be try this,
<option value="<?= $row["name"] ?>"><?= $row["name"] ."( $".$row["price"].")" ?></option>
check the argument on here
if(count($postOpts)) {
should be
empty($postOpts)
<select class="form-control" name="list" title="pick a type">
<?php
if(empty($postOpts)) {
foreach($postOpts as $row)
{
?>
<option value="<?php echo $row["name"] ?>">
<?php echo $row["price"] ?>
</option>
<?php
}
} else {
?>
<option value="<?php echo $post->getDefaultPrice() ?>">
<?php echo "Default Shipping Option - $".$post->getDefaultPrice() ?>
</option>
<?php
}
?>
</select>
Replace <option value="<?= $row["name"] ?>"><?= $row["price"] ?></option> with <option value="<?= $row['name'] ?>"><?= $row['price'] ?></option> and let me know what you are getting now ?
You need to use single quote inside double quote or vice versa.

How To Call Array from other File

i have a array from oher file php and show in select data html
this is my array
array.php
<?php
$category_attachment = array( "SPK" ,
"Justifikasi" ,
"PR" ,
"RAB" ,
"Proc" ,
"PO" ,
"Notulen" ,
"Sertifikat" ,
"BAUT" ,
"BAST" ,
"Tagihan" ,
"Other"
); ?>
and this myfile.php
<?php include "array.php" ?>
<select name="category" id="category">
<option value="">Choose Category</option>
// I want array data in here
</select>
Help Me Thank's
Try this
<select name="category" id="category">
<option value="">Choose Category</option>
<?php
foreach ($category_attachment as $value) {
?>
<option value="<?php echo $value ?>"><?php echo $value ?></option>
<?php
}
?>
</select>
just try print_r($category_attachment); in your myfile.php and see what happens. you should be able to access the variable after you included the file
Also, you should disable the first option by using the disabled attribute ;)
<?php
include "array.php"
print_r($category_attacment);
?>
<select name="category" id="category">
<option disabled>Choose Category</option>
<?php
foreach($category_attachment as $cat) {
echo "<option value='" . $cat . "'>" . $cat . "</option>";
}
?>
</select>
This example won't work:
<?php
print_r($category_attacment);
include "array.php"
?>
you have to include the file before you can access the variable.
You could do the following :
<select name="category" id="category">
<option value="">Choose Category</option>
<?php
foreach($category_attachment as $category){
echo '<option value="'.$category'">'.$category.'</option>';
}
?>
</select>
Give a look at foreach doc, which is very useful when using array :
http://php.net/manual/en/control-structures.foreach.php
You can do this:
<?php include "array.php" ?>
<select name="category" id="category">
<option value="">Choose Category</option>
<?php foreach ($category_attachment as $category) {?>
<option value="<?php echo $category; ?>"><?php echo $category; ?></option>
<?php } ?>
</select>
As simple as this,
<select name="category" id="category">
<?php
foreach ($category_attachment as $value) {
?>
<option value="<?= $value ?>"><?= $value ?></option>
<?php
}
?>
</select>

How to make dropdown option selected against a variable in php

I have a dropdown like the following.
<select>
<option value="Mr">Mr</option>
<option value="Dr">Dr</option>
<option value="Prof">Prof</option>
</select>
I am getting a value from data base in $selected_value variable. Based on this value I want to make one option from the above select to be selected.
Eg: If $selected_value = Mr, <option value="Mr" selected>Mr</option>
if $selected_value = Dr, <option value="Dr" selected>Dr</option>
update:
now when i am inspecting element i am getting like below.but not selecting Dr.but it is orking in w3schools try editor.
<select>
<option value="Mr">Mr</option>
<option value="Dr" selected="selected">Dr</option>
<option value="Prof">Prof</option>
</select>
update 2
see screen shot:
update3
now it works! added name for <select>
This will work for you problem
try this code
<select>
<option value="Mr" <?=($selected_value=="Mr") ? "selected" : ""?>>Mr</option>
<option value="Dr" <?=($selected_value=="Dr") ? "selected" : ""?>>Dr</option>
</select>
try this..
<option value="Mr" <?php if($selected_value == 'Mr') echo 'selected' ?>>Mr</option>
<option value="Dr" <?php if($selected_value == 'Dr') echo 'selected' ?>>Dr</option>
<option value="Prof" <?php if($selected_value == 'Prof') echo 'selected' ?>>Prof</option>
Or you can use by using jquery
<script>
$('select option[value="<?php echo $selected_value ?>"]').attr('selected','true')
</script>
As per your current HTML use the code below:
<select name="your_select_name">
<option <?php echo (($selected_value=="Mr")?"selected":"") ?> value="Mr">Mr</option>
<option <?php echo (($selected_value=="Dr")?"selected":"") ?> value="Mr">Dr</option>
<option <?php echo (($selected_value=="Prof")?"selected":"") ?> value="Mr">Prof</option>
</select>
If the value of this variable($selected_value) returns "Dr" then 2nd option will be selected. And also give a name of your select tag.
I changed my solution so it fits yours I hope.
UPDATE:
<select name="dropdownlist">
<?
$options = array("Mr", "Dr", "Prof");
foreach($options as $option){
if($_POST['dropdownlist'] == $option){
echo '<option selected="selected">' .$option. '</option>';
}else{
echo '<option>' .$option. '</option>';
}
}
?>
</select>
If you also have an array of your options you could create the option tags by looping through each one of them. In that loop you then can check if the $selected_value matches $option_value like so:
<select>
<?php foreach($options as $option_value => $option_displayName) : ?>
<option
value="<?php echo $option_value; ?>"
<?php echo $option_value == $selected_value ? 'selected' : ''; ?>>
<?php echo $option_displayName; ?>
</option>
<?php endforeach; ?>
</select>
To do what you want you'd have to build the select dynamically like the following example:
<?php
$selects = array(
array('name' => 'Mr', 'value' => 'Mr'),
array('name' => 'Dr', 'value' => 'Dr'),
array('name' => 'Prof', 'value' => 'Prof'),
);
$selected_option = 'Dr';
echo '<select>';
foreach($selects as $select) {
if($select['value'] == $selected_option) {
echo '<option value="'.$select['value'].'" selected>' .$select['name']. '</option>';
} else {
echo '<option value="'.$select['value'].'">' .$select['name']. '</option>';
}
}
echo '</select>';
?>
Which outputs:
<select>
<option value="Mr">Mr</option>
<option value="Dr" selected>Dr</option>
<option value="Prof">Prof</option>
</select>
Example
Try as follows .
<select name="select_options">
<option value="Mr">Mr</option>
<option value="Dr">Dr</option>
<option value="Prof">Prof</option>
</select>
After hit submit button. Then you will get value in $_POST['select_options']
There are many ways to achieve what you're doing. I can think of two straight ways to do this.
The first is ugly:
Insert in each option a php tag and check if value is selected:
<select>
<option <?php if ($selected_value == 'Mr') echo 'selected'; ?> value="Mr">Mr</option>
<option <?php if ($selected_value == 'Dr') echo 'selected'; ?> value="Dr">Dr</option>
<option <?php if ($selected_value == 'Prof') echo 'selected'; ?> value="Prof">Prof</option>
</select>
Otherwise, I would personally write a little helper
function generateSelect(array $entries, $selected)
{
$ret = '<select>'
foreach ($entries as $entry) {
$ret .= '<option';
if ($entry == $selected) {
$ret .= ' selected';
}
$ret .= ' value="'.$entry.'"';
$ret .= '>'.$entry.'</option'>;
}
return $ret;
}
It is just an example and its functionality could be expanded. It SHOULD work, but I haven't tried it myself (wrote it quickly)
You have mistaken syntax
<select>
<option value="Mr">Mr</option>
<option value="Dr" selected="selected">Dr</option>
<option value="Prof">Prof</option>
</select>
this will work
<select>
<option value="Mr">Mr</option>
<option value="Dr" selected>Dr</option>
<option value="Prof">Prof</option>
</select>

Categories