I Have this form,but whenever the form_validation return FALSE. The Dropdown form always set to default which was PLEASE SELECT.
How do i Set the value like the input form,i dont know where to put the set_value
Dropdown Form:
<div class="form-group">
<select class="form-control form-control-sm" id="exampleFormControlSelect1" name="jenisKelamin">
<option value="">PLEASE SELECT</option>
<?php foreach ($system as $d) : ?>
<?php if ($d->system_type=="JENIS_KELAMIN"): ?>
<option value="<?= $d->system_value_txt ?>"><?= $d->system_value_txt ?></option>
<?php endif ?>
<?php endforeach; ?>
</select>
<small class="text-danger"><?= form_error('jenisKelamin') ?></small>
</div>
To set a default dropdown menu, you could use the set_select() method on your option input like this :
<div class="form-group">
<select class="form-control form-control-sm" id="exampleFormControlSelect1" name="jenisKelamin">
<option value="">PLEASE SELECT</option>
<?php foreach ($system as $d) : ?>
<?php if ($d->system_type=="JENIS_KELAMIN"): ?>
<option value="<?= $d->system_value_txt ?>" <?php echo set_select('jenisKelamin', $d->system_value_txt); ?> ><?= $d->system_value_txt ?></option>
<?php endif ?>
<?php endforeach; ?>
</select>
<small class="text-danger"><?= form_error('jenisKelamin') ?></small>
</div>
Related
So when i press submit every other field with input is printed, but selected option tag isnt printed in html, since option values are stored in an array..
select forms :
<div class="form-group">
<select class="form-control" name="from">
<option selected disabled>--Isvykimo vieta</option>
<?php foreach($cities as $city):?>
<option value='' style="color:black;"><?=$city;?></option>
<?php endforeach;?>
</select>
</div>
<div class="form-group">
<select class="form-control" name="destination">
<option selected disabled>--Keliones tikslas</option>
<?php foreach($destinations as $destination):?>
<option value='' style="color:black;"><?=$destination;?></option>
<?php endforeach;?>
</select>
</div>
and when i try to post
<div class="ticket">
<?php if(isset($_POST['send'])) ?>
<div class="ticketInfo">
<h5><?php echo htmlentities($_POST['names']);?> <?php echo htmlentities($_POST['lastName']);?></h5>
<p>From: <?= $_POST['from'];?> To:<?= $_POST['destination'];?></p>
</div>
</div>
from to is just left blank
<option value="<?=$city;?>" style="color:black;"><?=$city;?></option>
your value of input was empty, try like this please
When I select the all data of a product by it's product_id. and load product data in a form for an update. but here I cannot set selected drop-down list value in this case.
here, is my view
<form action="<?php //echo base_url().'admin_product/pro_update/'?>" method="post" class="form-horizontal">
<div class="control-group">
<label class="control-label">Title :</label>
<div class="controls">
<input type="text" class="span5 m-wrap" placeholder="Product title" name="title" value="<?php echo $item[0]['p_title']?>" />
<h6 style="color:red"><?php echo form_error('title')?></h6>
</div>
</div>
<div class="control-group">
<label class="control-label">Category :</label>
<div class="controls">
<select name="cat" id="cat">
<option value="">--Select Category--</option>
<?php foreach($cat as $c):?>
<option value="<?php echo $c->cat_id?>"><?php echo $c->cat_name;?>//selected set value here</option>
<?php endforeach;?>
</select>
<h6 style="color:red"><?php echo form_error('cat')?></h6>
</div>
</div>
here is my controller code,
public function pro_edit($id)
{
$p['item']=$this->ap->product_update($id);
$p['cat']=$this->ap->up_cat();
$p['color']=$this->ap->up_color();
$this->load->view('layouts/header',$p);
$this->load->view('admin/product_update',$p);
$this->load->view('layouts/footer',$p);
}
and model code,
public function product_update($id)
{
$this->db->select('p_title,p_description,p_category,p_stock_quantity,p_pprice,p_sprice,p_size_variant,cat_name,v_color,v_size,p_id');
$this->db->from('tbl_product');
$this->db->join('tbl_category','tbl_product.p_category=tbl_category.cat_id');
$this->db->join('tbl_variant','tbl_product.p_color_variant=tbl_variant.v_id');
$this->db->where('p_id',$id);
$query=$this->db->get();
return $query->result_array();
}
Assumptions
p_category is your category id
Code
<select name="cat" id="cat">
<option value="">--Select Category--</option>
<?php
foreach($cat as $c):?>
<option value="<?php echo $c->cat_id?>" <?php echo ($item[0]['p_category'] == $c->cat_id) ? 'selected' : '' ?>>
<?php echo $c->cat_name;?>
</option>
<?php
endforeach;
?>
</select>
Explain
Q - What this if do in above <?php echo ($item[0]['p_category'] == $c->cat_id) ? 'selected' : '' ?>
A - Assume $item[0]['p_category'] has value 2 so when ever $c->cat_id equesl to that value its print selected keyword
Ex:
<select>
<option value="1">aa</option>
<option value="2" selected>bb</option>
<option value="3">cc</option>
<option value="4">dd</option>
</select>
I have a country dropdown
<select name="country" class="form-control"/>
<option>Select a Country</option>
<?php foreach($country_list as $country) :?>
<option value="<?php echo $account_result->Country;?>"
<?php
if($country->id==$account_result->Country)
{echo 'selected="selected"';};?>>
<?php echo $country->name; ?></option>
<?php endforeach; ?>
</select>
but while updating I am getting only selected value id it's not changing.
You setting same value to all options change this line:
<option value="<?php echo $account_result->Country;?>"
to:
<option value="<?php echo $country->id;?>"
Try below
You have added wrong value in option
<select name="country" class="form-control"/>
<option>Select a Country</option>
<?php foreach($country_list as $country) :?>
<option value="<?php echo $country->id;?>"
<?php
if($country->id==$account_result->Country)
{echo 'selected="selected"';};?>>
<?php echo $country->name; ?></option>
<?php endforeach; ?>
</select>
<?php echo $country->id == $account_result->Country ?"selected":"";?>
your $country->id and $account_result->Country should be same value example
ex: 44=44
then it will be get selected in default page load.
and you have to set the option value as like this.
<option value="<?php echo $country->id;?>"
I'm trying to insert a PHP script into a selected attribute in HTML5 ,but I have failed with syntax.
Error :"The value of selected attribute is made invalid because it's not supported by the current schema".
It looks like this... How can I solve this problem? Thanks.
<div>
<label>Select author here:</label>
<select name="author">
<option value="">Select an author</option>
<?php foreach ($authors as $author): ?>
<option value="<?php htmlout($author['id']);?>"
selected="<?php ?>">
<?php htmlout($author['name']);?>
</option>
<?php endforeach; ?>
</select>
</div>
try it :
<div>
<label>Select author here:</label>
<select name="author">
<option value="">Select an author</option>
<?php foreach ($authors as $author): ?>
<option value="<?php htmlout($author['id']);?>"
<?php echo $Your_Select_value == $author['id'] ? "selected" : '';?> >
<?php htmlout($author['name']);?>
</option>
<?php endforeach; ?>
</select>
</div>
Proper value of selected attribute is selected. Thus if you wish to apply this attribute to the option that you consider currently selected, try this (I assume that PHP variable that stores selected author's id is $authorid):
<div>
<label>Select author here:</label>
<select name="author">
<option value="">Select an author</option>
<?php foreach ($authors as $author): ?>
<option value="<?php htmlout($author['id']);?>"
<?php if ($author['id'] == $authorid) echo ('selected="selected"'); ?>
>
<?php htmlout($author['name']);?>
</option>
<?php endforeach; ?>
</select>
</div>
I have a piece of code which creates dropdown list of the countries
<div class="input-box">
<?php $_countries = Mage::getResourceModel('directory/country_collection')->loadData()->toOptionArray(false) ?>
<?php if (count($_countries) > 0): ?>
<select name="country" id="country" onchange="print()">
<option value=""> </option>
<?php foreach($_countries as $_country): ?>
<option value="<?php echo $_country['value'] ?>">
<?php echo $_country['label'] ?>
</option>
<?php endforeach; ?>
</select>
<?php endif; ?>
</div>
I can't figure out how to get the country which user has chosen. Can you please help me?
<div class="input-box">
<?php $_countries = Mage::getResourceModel('directory/country_collection')->loadData()->toOptionArray(false) ?>
<?php if (count($_countries) > 0): ?>
<select name="country" id="country" onchange="showCountry(this)">
<option value=""> </option>
<option value="<?php echo $_country['value'] ?>" <?php echo $select; ?> >
<?php echo $_country['label'] ?>
</option>
<?php endforeach; ?> </select>
<?php endif; ?>
</div>
function showCountry(ele){
alert(ele.value);
}