save selected value in database as id - php

i have two tables as shown, when employee edits its details department_id save as 0 in database
employee department
id| name|department_id id|department_name
1 |abc |1 1|HR
2 |xyz |4 2|Finance
3 |asd |3 3|Developer
and this view part
<tr>
<th> Department </th>
<td>
<SELECT name="department_id" style="width:180px;" >
<?php
foreach($departments as $row): ?>
<option value="<?php $employee['department_id'] ;?>"
<?php if ($row['id']== $stakehholder['department_id'])
{
echo 'selected';
}
?>>
<?php echo $row['department_name'];?>
</option>
<?php endforeach ?>
</SELECT>
</td>
<td> <?php echo form_error('department_id'); ?> </td>
</tr>
after edit 0 value get store in database

forget echo <option value="<?php $employee['department_id']; ?>"
<SELECT name="department_id" style="width:180px;" >
<?php foreach($departments as $row): ?>
<option value="<?php echo $row['department_id']; ?>"
<?php if ($row['department_name']== $stakehholder['department_id']) {
echo 'selected';
}
?>>
<?php echo $row['department_name']; ?>
</option>
<?php endforeach ?>
</SELECT>

updated line is in as below
<option value="<?php echo $employee['department_id'] ;?>
<tr>
<th> Department </th>
<td>
<SELECT name="department_id" style="width:180px;" >
<?php
foreach($departments as $row): ?>
<option value="<?php echo $employee['department_id'] ;?>"
<?php if ($row['department_name']== $stakehholder['department_id'])
{
echo 'selected';
}
?>>
<?php echo $row['department_name'];?>
</option>
<?php endforeach ?>
</SELECT>
</td>
<td> <?php echo form_error('department_id'); ?> </td>
</tr>

you have forgot to echo department_id in option value attribute and second you are comparing name to id which never match if id is int and name not.
try this updated code:
<SELECT name="department_id" style="width:180px;" >
<?php
foreach($departments as $row): ?>
<option value="<?php echo $row['department_id'] ;?>"
<?php if ($row['department_id']== $stakehholder['department_id'])
{
echo 'selected';
}
?>>
<?php echo $row['department_name'];?>
</option>
<?php endforeach ?>
</SELECT>

Related

Why I can't use chained jquery in loop condition?

<?php
foreach ($employee as $data) {
?>
<tr>
<td>
<form action="<?php echo base_url();?>controller_admin/updateEmployee/<?php echo $data->id_employee;?>" method="post">
<select style="width: 100px; height: 33px;" name="directorate" id="directorate">
<option selected value="<?php echo $data->id_directorate ?>"><?php echo $data->directorate ?></option>
<?php
foreach ($directorate as $key) {
if ($data->directorate != $key->directorate) {?>
<option value="<?php echo $key->id_directorate ?>">
<?php echo $key->directorate; ?>
</option>
<?php
}
}
?>
</select>
</td>
<td>
<select style="width: 100px; height: 33px;" name="department" id="department">
<?php if ($data->id_department!=null) {?>
<option value="0"></option>
<?php
} ?>
<option selected value="<?php echo $data->id_department ?>"><?php echo $data->department; ?></option>
<?php
$value = 1;
foreach ($department as $key) {
if ($data->department != $key->department) {?>
<option
class="<?php echo $key->id_directorate ?>"
value="<?php echo $value ?>">
<?php echo $key->department; ?>
</option>
<?php
$value++;
}
}
?>
</select>
</td>
<td>
<select style="width: 100px; height: 33px;" name="section" id="section">
<?php if ($data->id_section!=null): ?>
<option value="0"></option>
<?php endif ?>
<option selected value="<?php echo $data->id_section ?>"><?php echo $data->section; ?></option>
<?php
$value = 1;
foreach ($section as $key) {
if ($data->section != $key->section) {?>
<option
class="<?php echo $key->id_department ?>"
value="<?php echo $value ?>">
<?php echo $key->section; ?>
</option>
<?php
$value++;
}
}
?>
</select>
</td>
<td>
<button type="submit" class="btn btn-warning"><i class="fa fa-edit"></i></button>
<button type="button" class="btn btn-danger"><i class="fa fa-trash-o"></i></button>
</form>
</td>
<?php
}
?>
<script src="<?php echo base_url();?>assets/js/jquery.chained.min.js"></script>
<script>
$("#department").chained("#directorate");
$("#section").chained("#department");
</script>
I want to make a list of table to display chained dropdown to display like this:
and chained dropdown work like this:
but, it will display in first row only, at another row, it does not work.
You can look like this:
I hope I can chained dropdown for all rows. Anyone find the solution?
you could try to put your js code inside the ready function - its possible that the elements are not even rendered when you try to select them.
<script>
$( document ).ready(function() {
$("#department").chained("#directorate");
$("#section").chained("#department");
});
</script>
reference: http://learn.jquery.com/using-jquery-core/document-ready/

Update Dropdown Value

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;?>"

Onchange event after selecting in select dropdown list in ZEND

I have a view with a dropdown list using select tag and I want to how can i have an event or an onchange event after selecting a value in my dropdown.
here is my code in index.phtml
<?php if (count($users)): ?>
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>E-mail</th>
<th>Level</th>
<th></th>
</tr>
</thead>
<?php foreach ($users as $user): ?>
<tr>
<td><?php echo $this->escapeHtml($user->first_name . ' ' . $user->last_name); ?></td>
<td><?php echo $this->escapeHtml($user->email); ?></td>
<td>
<select name="level">
<?php $level = $this->escapeHtml($user->level); ?>
<?php if ($level == 1): ?>
<option value="1" selected="selected">Administrator</option>
<option value="2">Manager</option>
<option value="3">HR Staff</option>
<?php elseif ($level == 2): ?>
<option value="1" >Administrator</option>
<option value="2" selected="selected">Manager</option>
<option value="3">HR Staff</option>
<?php elseif ($level == 3): ?>
<option value="1" >Administrator</option>
<option value="2" >Manager</option>
<option value="3" selected="selected">HR Staff</option>
<?php endif; ?>
</select>
</td>
<td style="text-align: right;">
<i class="icon-remove icon-white"></i>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php else: ?>
<h3>There are no registered users available.</h3>
<?php endif; ?>
Thanks in Advance
create javascript function and put function in select box onchange event.
check working example on fiddle
<script>
function changeTest(obj){
alert(obj.options[obj.selectedIndex].value);
}
</script>
<select name="level" onChange="changeTest(this)">
<option value="1" selected="selected">Administrator</option>
<option value="2">Manager</option>
<option value="3">HR Staff</option>
</select>

Dynamic dropdown list in a HTML form

I have the following code to create a drop-down list:
<body>
<form method="post">
<table>
<tr>
<td>Firm Name:</td>
<td><input type="text"class="input_text_long" name="name" value="<?php echo $name ?>"/></td>
</tr>
<tr>
<td>Hub Name:</td>
<select>
<option value="">---Select---</option>
<?php
$list=mysql_query("SELECT * FROM hub");
while($row = mysql_fetch_assoc($list)) {
?>
<option value=<?php echo $row_list['name'];?>
</option>
<?php } ?>
</select>
</tr>
<td> </td>
<td><input type="submit" name="save" value="Save" /></td>
</tr>
</table>
</form>
</body>
This, however, does not display any dropdown list. It only displays a text box. Can anyone tell me what I am doing wrong, please? Or how to create a dropdown box in a form.
Your option tag is wrong.
Right syntax <option value="VALUE">OPTION NAME</option>
<select>
<option value="">---Select---</option>
<?php
$list = mysql_query("SELECT * FROM hub");
while ($row = mysql_fetch_assoc($list)) {
$name = $row['name'];
?>
<option value="<?php echo $name; ?>"><?php echo $name; ?></option>
<?php
}
?>
</select>
Change this line
<option value=<?php echo $row_list['name'];?></option>
to
<option value="<?php echo $row['name'];?>"><?php echo $row['name'];?></option>
I think the issue is with this line:
<?php echo $row_list['name'];?>
It should be:
<?php echo $row['name'];?>
Also, your opening <option> tag does not have a closing >
<option value=<?php echo $row['name'];?>></option>

Magento: Get country that user has chosen

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);
} ​

Categories