i want to get the id of selected option in dropdown.
I have a dropdow that displays company names from database. Now I want get the id of that company. How do i do that?
<select required name="org-list" id="org-list" class="form-control">
<option value="">Select</option>
<?php foreach($org as $value) { ?>
<option id="org" value="<?php echo $value['org_name'];?>"><?php echo $value['org_name']; ?></option>
<?php } ?>
</select>
The Model
public function get_organisation()
{
$q = $this->db->select('*')
->from('company')
->get();
return $q->result_array();
}
And in my controller i want the id of selected option from database.
$data = $this->key_m->array_from_post(array('id','org-list','keys'));
$data['keys'] = $license;
var_dump($data);
You should put the organization id in option tag's value. like this
<option value="<?php echo $value['org_id'];?>"><?php echo $value['org_name']; ?></option>
No need to provide id attribute to each option tag. When you will submit the form value of "org-list" will be selected organization id.
This is the ajax way
$.post(url,{ id : "id value from drop down here" },function(data){
//This is the call back for success
})
For that one, you need jquery. The is the easiest way to use. Url is where you are posting back to server.
In your server side catch like this
$this->input->post('id')
Because you used "id" in the ajax request. So you have to retrieve it using the field name 'id' . Hope you get it.
There are a couple things which you "should" / "could" change. At first I noticed that your ID attribute is "org", Id's are supposed to be unique and yours is not. Though, back to the actual question. You should change your code as follow:
<select required name="org-list" id="org-list" class="form-control">
<option value="">Select</option>
<?php foreach($org as $value): ?>
<option id="<?php echo $value['org_id'];?>" value="<?php echo $value['org_id'];?>"><?php echo $value['org_name']; ?></option>
<?php endforeach; ?>
</select>
Now in your controller you grab the selected ID like: $this->input->post('org-list').
You have to provide value for each id attr. for corresponding <option>
<select required name="org-list" id="org-list" class="form-control">
<option value="">Select</option>
<?php foreach($org as $value): ?>
<option id="<?php echo $value['org_id'];?>" value="<?php echo $value['org_name'];?>"><?php echo $value['org_name']; ?></option>
<?php endforech; ?>
</select>
Related
Currently setting up a form for users to select aircraft information from. Currently, the form allows users to select an airplane and in return when the form is submitted it passes the value"aircraft type" to an API. Due to this, I cannot set the values to be the registration of the airplanes so have put this into the ID for now (open to suggestions).
<select name="type" id="plane-list">
<option selected="selected">Choose one</option>
<?php
foreach($allaircraft as $item){
?>
<option id="<?php echo $item->registration; ?>" value="<?php echo $item->icao; ?>"><?php echo $item->name; ?> - <?php echo $item->registration; ?></option>
<?php
}
?>
</select>
As you can from above these are all dynamic values which change over time so hard coding the options are not possible.
So summed up I need to pull the ID or $item->registration; instead of ICAO code. This then needs to be set as a variable so that I can call a function with this code.
Let me know if you need me to clear up anything. Don't normally questions on here so be nice.
I need to make one of the form into read only based on user's division
here's my form in my view:
<select class="form-control" name="division" id="division">
<option selected="selected" disabled="disabled" >-Choose-</option>
<option selected="selected" value="all">All</option>
<?php
if($query->num_rows()) {
foreach($query->result() as $row):
?>
<option value="<?php echo $row->id_division?>">
<?php echo $row->name_division;?>
</option>
<?php
endforeach;
}
?>
</select>
Is there any way to make it read only based on the user's id_division? So once they click submit or something this will also input their own id_division
I guess from your question that there are more than one $id_division, and more than one <option> in the select dropdown. In that one $id_division belongs to the current user.
While submitting form, value of this current user's $id_division only need to pass. In other words, by default the dropdown value should be automatically set for the option which belongs to the current user.
For that case you have to check each &id_division in the foreach loop whether its equal to the id_division of current user. And add the selected attribute dynamically to the <option> if it equals.
for that,
<?php
if($query->num_rows()) {
foreach($query->result() as $row):
if($id_division==[id_division of current user]){//You have to replace [id_division of current user] with variable that you keep current user id_division data.
$selected='selected';
}else{
$selected='';
}
?>
<option value="<?php echo $row->id_division?>" <?php echo $selected;?>>
<?php echo $row->name_division;?>
</option>
<?php
endforeach;
}
I have a dropdown box where the data is coming from my database. I want to send the selected drop-down item to the controller action method. And then I will send these values to the model to do the further works. Here, the problem is I have two database values in a single item of drop-down box. And, I am not figuring out how to send those two the method. Here is my code given below,
<select name='select'>
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option>Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationid']; ?></option>
<?php endforeach; ?>
</select>
From this dropdown items, I want to send the sourcestationid and destinationstationid separately as 2 parameters to my controller action method. Here is my controller code though this is not correct way I think,
function getdata(){
$iotdata['test'] = $this->input->post('select2');
//rest of the code according to the source and destination id item
}
Thanks in advance.
You haven't kept the variable inside option's value tag.
<select name='select'>
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option value="<?php echo $get_stations_item['sourcestationid'].','.$get_stations_item['destinationstationid']; ?>">Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationid']; ?></option>
<?php endforeach; ?>
</select>
Also here at PHP end, you can explode the string into an array and store it further into db.
<?php
$select = explode(',', $select)
print_r($select); // this will have your two values
?>
Thanks #BitsPlease for our suggestion. Your idea is working. But I need to do a small change to get it instantly. My select tag needs to be under form tag to get them without reloading the page again. Here is my view,
<form method="post" accept-charset="utf-8" action="<?php echo site_url("controller/action"); ?>">
<select name='select' onchange="this.form.submit()">
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option value="<?php echo $get_stations_item['sourcestationid'].','.$get_stations_item['destinationstationidr']; ?>">Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationidr']; ?></option>
<?php endforeach; ?>
</select>
</form>
Here is the php code,
$select = explode(',', $this->input->post('select'));
print_r($select)
I have a create.php which creates a user, and edit.php for editing information of the student.
I'm a beginner in PHP so if this might be a question that has a very easy method, please do tell.
I've been searching for retrieving a value that has already been set on a dropdown, but the only things I've found are:
a.) They only print the selected value, not retrieve the already-set option.
sample code:
<p>Schedule</p>
<select value="<?= $data->schedule; ?>" name = "schedule">
<?php
$schedule = array ('Morning', 'Afternoon');
foreach ($schedule as $sched) { ?>
<option value="<?php echo $sched; ?>"><?php echo $sched; ?></option>
</select>
<?php } ?>
b.) I have no knowledge of javascript, I tried it, but as I have no knowledge of it, I can't seem to make it work.
sample codes I tried:
<p>Schedule</p>
<select id = "my_select" name = "schedule">
<option id = "o1" id="id1">Morning</option>
<option id = "o2" id="id2">Afternoon</option>
</select>
<script type="text/javascript">
$("#my_select").change(function() {
var id = $(this).find('option:selected').attr('id');
});
</script>
I also tried
document.getElementById('schedule').selectedOptions[0].text
but still does not output "afternoon"
This is my last resort as I have no other resource to find.
I added a picture of what I wanted to do since my question might be a little confusing.
Selects use selected attribute not the value to define the selected state.
So in your code check the value and then apply selected.
<p>Schedule</p>
<select name="schedule">
<?php foreach (['Morning', 'Afternoon'] as $sched): ?>
<option value="<?= $sched ?>"<?= ($data->schedule == $sched ? ' selected' : null) ?>><?= $sched ?></option>
<?php endforeach ?>
</select>
In my dropdownlist I have two different values for each option. How can I retrieve both? Let me illustrate what I mean.
<select name="my_ddl">
<option value="<?php $value_Id ?>"><?php $value_text ?></option>
<option value="<?php $value_Id ?>"><?php $value_text ?></option>
</select>
When the form is posted, I want to be able to get both the $value_id and $value_text of the selected option. How can I do this?
$_POST['my_ddl'] only gets one value not both.
In asp.net I could do this simply by referring to my_ddl.Value and my_ddl.Text.
Thank you!
Strictly, this is not possible.
What you could do is use a delimiter in your value attribute:
<select name="my_ddl">
<option value="<?php echo $value_Id ?>|<?php echo $value_text ?>"><?php echo $value_text ?>
</option>
</select>
And...
<?php
list($id, $text) = explode('|', $_POST['my_ddl']);
//...
?>
Another strange way of doing it is:
<select name="my_ddl">
<option value="<?php echo $value_Id ?>[<?php echo $value_text ?>]">
<?php echo $value_text ?>
</option>
</select>
Then when you process it you can do this or maybe even something more simple:
foreach ($_POST['my_dd1'] as $value_Id => $value_text) {
$value_Id = $value_Id;
$value_text = $value_text;
}
Because php treats the [] as meaning the string is an array and so you instantly have an associative array. I agree though that if you put it there in the first place you ought to be able to just look it up again in the code rather than rely on this.
If you're using PHP to populate the text for the <option> then you can probably just look the value up on the server. Perhaps you just need to use the $value_id to look up the text in a database table?
If not, you could include a hidden field on your form and use JavaScript to update that hidden field with the text every time a new value is selected.
You cannot get value_text from POST data. One solution is to populate the hidden field after choosing the option via JavaScript.