Select2 does not show the set value - jquery-select2-4

There is a select2 element :
<select name="commande_id" id="commande">
<option value=""> -- Choisir une commande -- </option>
<option value="1">COM-2-2015</option>
</select>
$(document).ready(function() {
...
$("#commande").select2({width:'100%'});
...
});
When I enter the page in update mode then I set the option value of the select element programmatically to be the value from database :
$(document).ready(function() {
...
<?php
if (isset($data)) { ?>
...
$("#commande").val("<?php echo $data[0]['commande_id'];?>");
...
<?php
}
?>
...
});
The problem is that at runtime the select element does not show the appropriate text :
But when I click it then the appropriate value is selected :
So how to make the appropriate text shown ?

I don't know how you are getting the $data, but I guess it's something like that, just an example:
<?php
$data[] = ["commande_id" => "1", "name_field" => "COM-2-2015"];
foreach ($data as $key => $value){
?>
<select>
<option value='<?php echo $value['commande_id']?>'><?php echo $value['name_field'] ?></option>
</select>
<?php } ?>
Look it here: https://ideone.com/PSpKmu

Related

Drop down item not being selected by default in Razor view

I have the following code written in Razor view in MVC in C#. I want to select one of the item based on value but it is not working. I confirmed that the variable Facilities contain value Un-Funded but it doesn't select this item.
<select id="facilities" class="form-control" style="width:200px;">
<option #{ if (Facilities == "Funded") { Response.Write(" selected "); } }>Funded</option>
<option #{ if (Facilities == "Un-Funded") { Response.Write(" selected "); } }>Un-Funded</option>
</select>
Here's screenshot of how HTML looks like. Notice it is not printing selected for any of the option.
I am using Response.Write which doesn't work in Razor view. Using #Html.Raw("some string") fixed the issue. So the code will look like this:
#{ if(Facilities == "Funded") { #Html.Raw("selected='selected'") } }
You can try to use js,so that if you have many options,you don't need to check on each option:
<select id="facilities" class="form-control" style="width:200px;">
<option value="Funded">Funded</option>
<option value="Un-Funded">Un-Funded</option>
</select>
<script>
$(function () {
$("#facilities").val("#Facilities");
//Or you can use $("#facilities").val(#Html.Raw(Json.Encode(Facilities)));
})
</script>
With MVC 5 you can use this method #: to insert your selected:
<select id="facilities" class="form-control" style="width:200px;">
<option value="Funded" #{ if (Facilities == "Funded") { #: selected="selected"
}}>
Funded
</option>
<option value="Un-Funded" #{ if (Facilities == "Un-Funded") { #: selected="selected"
}}>
Un-Funded
</option>
</select>
for older version you can check this link :
setting-the-selected-option-in-mvc3

Targeted Time Zones List / Options

The existing code that I am working with provides a drop-down for the end
user to select a time zone.
My goal is to limit the choices down to
1. America
and
a. Option Eastern
value America/New_York
b. Option Central
value America/Chicago
c. Option Mountain DST
value America/Denver
d. Option Mountain no DST
value America/Phoenix
e. Option Pacific
value America/Los_Angeles
Narrowing it down to America was easy enough by simply commenting out the code.
Narrowing it down further to the 5 time zones is turning out to be beyond my pay grade.
Any advice?
$regions = [
# 'Africa' => DateTimeZone::AFRICA,
'America' => DateTimeZone::AMERICA,
# 'Antarctica' => DateTimeZone::ANTARCTICA,
# 'Aisa' => DateTimeZone::ASIA,
# 'Atlantic' => DateTimeZone::ATLANTIC,
# 'Europe' => DateTimeZone::EUROPE,
# 'Indian' => DateTimeZone::INDIAN,
# 'Pacific' => DateTimeZone::PACIFIC
];
$timezones = [];
foreach ($regions as $name => $mask) {
$zones = DateTimeZone::listIdentifiers($mask);
foreach ($zones as $timezone) {
$timezones[$name][$timezone] = $timezone;
}
}
$actualTimezone = JFactory::getSession()->get('user-timezone', $displayData['dateHelper']->getDate()->getTimezone()->getName(), 'Calendar');
?>
<form action="<?php echo JUri::base(); ?>" method="get" class="dp-timezone dp-form">
<span class="dp-timezone__label">
<?php echo $displayData['translator']->translate('COM_CALENDAR_CHOOSE_TIMEZONE'); ?>:
</span>
<select name="tz" class="dp-select dp-timezone__select">
<option value="UTC"<?php $actualTimezone == 'UTC' ? ' selected' : ''; ?>>
<?php echo $displayData['translator']->translate('JLIB_FORM_VALUE_TIMEZONE_UTC'); ?>
</option>
<?php foreach ($timezones as $region => $list) { ?>
<?php foreach ($list as $timezone => $name) { ?>
<option value="<?php echo $timezone; ?>"<?php echo $actualTimezone == $timezone ? ' selected' : ''; ?>>
<?php echo $displayData['translator']->translate($name); ?>
</option>
<?php } ?>
<?php } ?>
</select>
<input type="hidden" name="task" value="profile.tz" class="dp-input dp-input-hidden">
<input type="hidden" name="option" value="com_calendar" class="dp-input dp-input-hidden">
<input type="hidden" name="view" value="profile" class="dp-input dp-input-hidden">
<input type="hidden" name="return" value="<?php echo base64_encode(JUri::getInstance()->toString()); ?>" class="dp-input dp-input-hidden">
</form>

Problems with $_POST and variable

I'm new to html and php and so on... Just self studying some things. Now I have a little problem with this form (inside of "auswahlmenue.php"):
<form action="liste.php" method="post">
Mitarbeiter
<select name="mitarbeiter">
<?php
// SQL-Query erzeugen
require "sql.php";
$sql01 = "SELECT NAME FROM felder ORDER BY name";
$result = mysql_query($sql01);
// für jeden Eintrag ein Option-Tag erstellen
while ($row = mysql_fetch_array($result)) {
echo '<option value="'.$row['ID'].'"'.($_POST['NAME'] == $row['ID'] ? " selected": "").'>'.$row['NAME'].'</option>';
}
?>
</select>
<input type="submit" value="Senden" />
</form>
Everything works fine. I see my dropdown list and I can select my name... Now I want to check my selection at this page (liste.php):
<?require "auswahlmenue.php";if(isset($_POST['mitarbeiter']) ) {$listemid=$_POST["mitarbeiter"];echo "Test $listenmid";}else {echo "Es wurden keine Werte übergeben";}?>
It doesn't print out my variable $listenmid... What am I getting wrong?
Thank you very much for your help...

Can smarty do like foreach in codeigniter

this is sample foreach in codeigniter
<select>
<?php
$no = 0;
foreach($value as $v){
?>
<option value=<?=$v->id?>><?=$v->name." -- ".$buy[$no]->currency;?></option>
<?php
$no++;
} ?>
this is sample code in smarty
<select>
{foreach from="$value" item="v"}
<option value="{$v.id}" >"{$v.name} -- $buy[0].currency"</option>
{/foreach}
</select>
i want $buy like this
$buy[0]
$buy[1]
$buy[3]
etc...

How to create grouped form fields in symfony 1.4

I want to group the form fields like field set or simply enclosed by div. My form needs to be look like below
<form>
<div class="step-1">
Field 1
Field 2
</div>
<div class="step-2">
Field 3
Field 4
</div>
</form>
Graphical example :
Edit : Form class added for reference!
class ProfileForm extends BaseProfileForm
{
public function configure()
{
..... // other widget configuration
$this->embedForm('media', new MediaForm());
}
}
How can I do this in symfony form?
For example:
In action:
$this->form = new MyCoolForm()
In templates:
<form name="form name" id="MyCoolForm" action="<?php echo url_for('action_url') ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
<?php echo $form['name']->render() ?>
<?php echo $form['name']->renderError(); ?>
<fieldset>
<legend>Ppassword:</legend>
<?php echo $form['password']->render() ?>
<?php echo $form['password']->renderError(); ?>
<?php echo $form['password_again']->render() ?>
<?php echo $form['password_again']->renderError(); ?>
<fieldset>
<?php echo $form->renderHiddenFields(); ?>
</form>
etc...

Resources