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;
}
Related
I'm struggling with making a dropdown list work in wordpress & PHP which includes a default option and then a list of different areas.
What I am trying to do is:
Initially set my default option as selected.
If the form has been submitted, add the selected attribute to the selected item and delete the selected attributed from the default option.
Reset the selected option to default when the page is being reloaded.
I have come up with the code below, but the only thing that is working so far is that the previously selected option is being kept after submit.
<select name="by_area">
<option value="default" disabled="" <?php if (!isset($_POST['by_area'])) { echo "selected";} ?>>select option</option>
<?php foreach($categories as $value) :?>
<option
value="<?php echo $value->slug; ?>"
<?php echo (isset($_POST['by_area']) && $_POST['by_area'] === $value->slug ) ? 'selected' : ''; ?>><?php echo $value->name; ?>
</option>
<?php endforeach; ?>
</select>
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 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>
currently i am working on a system. here i have a drop down list and its values are populating from database. now what i need is when ever some one selects a record from the list the selected value should be get stored in a session variable which ll get displayed in an another page. can we do so. i am coding in php.
my code is:
<td align="center">
<?php
$sql_dep=" Select * from places_tbl";
$row_dep = $db->Execute($sql_dep);
$total_dep = $row_dep->RecordCount();
?>
<select name="place" class="txtbx08" id="place">
<option value="">--Please Select City--</option>
<?php if($total_dep>0) {
while(!$row_dep->EOF)
{
?>
<option value="<?php echo $row_dep->fields["place_id"];?>">
<?php echo ucfirst($row_dep->fields["place_name"]); ?>
</option>
<?php
$row_dep->MoveNext();
}}
?>
</select></td>
I'm not sure how you're retrieving the selected value (assumingly through POST), but the normal procedure would be:
session_start();
$_SESSION['place'] = $_POST['place'];
i have this code right here:
<select name="group">
<option value="">Choose a group....</option>
<?php foreach($groups as $group):?>
<option value="<?php echo $group['groupID']?>" selected="yes"><?php echo $group['name']?></option>
<?php endforeach;?>
</select>
my question is how would i code the 'option' tag in my dropdown so that when i edit an existing data, the selected group(admin, users, moderators) of the data i will be editing will appear when i edit it.thanks.
The selected="yes" HTML attribute of the option tag should be only one. You need to put it on the right group.
Set a PHP variable like $selected_group that is true if the group is the right one, and print the selected attribute only for that group.
Set it to false instead.
For example, if your selected group id is putted as a request parameter called groupID you should use the code below:
<select name="group">
<option value="">Choose a group....</option>
<?php foreach($groups as $group):?>
<?php if ($group['groupID'] == $_REQUEST['groupID']) $selected_group = true; else $selected_group = false; ?>
<option value="<?php echo $group['groupID']?>" <? if ($selected_group) echo 'selected="yes"'; ?>><?php echo $group['name']?></option>
<?php endforeach;?>
</select>
You could set the chosen groupid as a array member inside $groups, for example:
$groups[0]['selected'] = true;
In this case change the line inside the loop like this one:
<?php if ($group['selected']) $selected_group = true; else $selected_group = false; ?>