I want to get multiselsect values in PHP. This is my code:
<select id="tableset" class="form-control select2 table_id" name="table_id[]">
<option value="">Table</option>
<?php foreach ($tables as $tbls) { ?>
<option value="<?php echo $tbls->id; ?>"><?php echo $tbls->name; ?></option>
<?php } ?>
</select>
now to receive the values this is what i am doing
foreach ($_GET['table_id'] as $selectedOption)
{
echo $selectedOption;
}
I get an error with invalid argument for foreach. Please help me. How can I sort the issue?
First for a multiple select you need to indicate it by adding multiple tag to the select
<select id="tableset" multiple="multiple" class="form-control select2 table_id" name="table_id[]">
<option value="">Table</option>
<?php
foreach ($tables as $tbls) {
?>
<option value="<?php echo $tbls->id; ?>"><?php echo $tbls->name; ?>
</option>
<?php } ?>
now you can get them
foreach ($_GET['table_id'] as $selectedOption){
echo $selectedOption;
}
Related
I have some options to select and would like to set some textinputs to visible or hidden depending on what user selects. I have the functionality already but in this case I need to take the second value of the echo and check if its equal to something, I can currently only check the first value of the echo:
<select id="type_id" name="device_type_id" onchange="if (this.value==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
So, I need to get ONLY the value of $row['serial_or_imei'] and compare onchange="if (this.value=='') , how can I do that?
EDIT: further explanation
I need to read the value of $row['serial_or_imei'] which could be empty, 'serial' OR 'imei', so, one of these three.
P.S: I must keep the $row['id'] in the value attribute because I am passing that value later to server so dont suggest removing that.
You can add one attribute to option element and test with it. something like
<select id="type_id" name="device_type_id" onchange="if (this.getAttribute('data')==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option data="<?php echo $row['serial_or_imei'] ?>" value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
You can compare the value of an array in the onchange by assign the value to a variable which will be a quite neat format as
<?php $value = echo $row['serial_or_imei']; ?>
<select id="type_id" name="device_type_id" onchange="if (this.value=='<?php empty($value)?>'){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
You add a new custom attribute serial and supply the value of serial_or_imei to it and then compare in javascript with onchange="if (this.value=='')
<select id="type_id" name="device_type_id" onchange="if (this.value==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>" data-serial="<?php echo $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
EDIT: Attribute “serial” is not allowed on element “option”. Changed this to data-seial.
You say you need the values back to server because they change dynamically. A clean way would be to have a dedicated hidden field:
<?php foreach ($result as $row) { ?>
<input type="hidden" name="map[<?php echo $row['id']; ?>]" value="<?php echo $row['serial_or_imei']; ?>">
<? } ?>
Here's a full self-contained script to illustrate it:
<?php
$random_serial_or_imei = range(1000, 9999);
shuffle($random_serial_or_imei);
$result = [];
for ($i = 0; $i < 5; $i++) {
$result[] = [
'id' => $i,
'serial_or_imei' => $random_serial_or_imei[$i],
'name' => "Name #$i",
];
}
?>
<select name="device_type_id">
<option value="">Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id']?>"><?php echo $row['name']; ?></option>
<?php } ?>
</select>
<?php foreach ($result as $row) { ?>
<input type="hidden" name="map[<?php echo $row['id']; ?>]" value="<?php echo $row['serial_or_imei']; ?>">
<? } ?>
This prints something like:
<select name="device_type_id">
<option value="">Select</option>
<option value="0">Name #0</option>
<option value="1">Name #1</option>
<option value="2">Name #2</option>
<option value="3">Name #3</option>
<option value="4">Name #4</option>
</select>
<input type="hidden" name="map[0]" value="9513">
<input type="hidden" name="map[1]" value="3931">
<input type="hidden" name="map[2]" value="3971">
<input type="hidden" name="map[3]" value="4476">
<input type="hidden" name="map[4]" value="6429">
Selected type can be found at $_POST['device_type_id'] and the corresponding IMEI at $_POST['map'][ $_POST['device_type_id'] ].
Based on the information from the comments, I think this solution is what you are looking for. Note I move the onchange to a function for clarity.
You can see the "three" option hides the text input as it doesn't have a imei.
Also notice how I use the data- attribute inside your option so you can retrieve that information as well, independent of the option id. Also note the this.form['device_imei_textinput'] didn't worked. The elements reference is required.
function check_imei(obj, form) {
var imei = obj.options[obj.selectedIndex].getAttribute('data-imei');
if (imei == '') {
form.elements.device_imei_textinput.style.visibility='hidden';
} else {
form.elements.device_imei_textinput.style.visibility='visible';
}
}
<form>
<select id="type_id" name="device_type_id" onchange="check_imei(this, this.form);">
<option value='Select'>Select</option>
<option value="1" data-imei="foo">One</option>
<option value="2" data-imei="bar">Two</option>
<option value="3" data-imei="">Three</option>
</select>
<input type="text" id="device_imei_textinput" name="device_imei_textinput" />
</form>
Mirror of example : https://jsfiddle.net/asp1hewx/
EDIT : To address the PHP side of things, the form above could be generated with :
<form>
<select id="type_id" name="device_type_id" onchange="check_imei(this, this.form);">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>" data-imei="<?php $row['serial_or_imei'] ?>"><?php echo $row['name'] ?></option>
<?php } ?>
</select>
<input type="text" id="device_imei_textinput" name="device_imei_textinput" />
</form>
I am trying to populate a drop-down list of the database. In my view file I have the following code
Here is my controller
$query = $this->interprete_model->interpreteID($this->session->userdata('user_id'));
print_r($query);
$data['interprete'] = $query;
Aqui esta mi vista, usa set_select.
<select class="form-control" name="regionI" id="regionI">
<option value="">- Select -</option>
<?php foreach($result as $row):?>
<option value="<?php echo $row->id;?>"
<?php echo set_select('regionI', $row->id, TRUE); ?>><?php echo $row->name;?></option>
<?php endforeach; ?>
</select>
Result:
enter image description here
Many selected, I need one selected to modify (update) the data.
You can try this :
<select class="form-control" name="regionI" id="regionI">
<option value="">- Select -</option>
<?php foreach($users as $row):
$selected = FALSE;
// 1 is the id u want to be selected u can change it according to you
if ($row->id == 1){
$selected = TRUE;
}
?>
<option value="<?php echo $row->id;?>"
<?php echo set_select('regionI', $row->id, $selected); ?>><?php echo $row->name;?></option>
<?php endforeach; ?>
</select>
You can also use form_dropdown as
// FOR ids
$ids = array(1,2,3,4); // array of user ids
echo form_dropdown('regionI',$ids,1,array('class'=>'form-control'));
// FOR name
$names= array('name1','name2','name4','name3'); // array of user names
echo form_dropdown('regionI',$names,'name1',array('class'=>'form-control'));
For More :
https://www.codeigniter.com/user_guide/helpers/form_helper.html
i write this way for edit time selection
<?php foreach ($select_single as $select_single_show):?>
<select class="form-control" name="regionI">
<?php foreach ($users as $row):?>
<option <?php if($row->id==$select_single_show->regionI)echo "selected";?> value="<?php echo $all_branch_show->id?>"><?php echo $row->name?>
</option>
<?php endforeach;?>
</select>
<?php endforeach;?>
I am using chosen select drop down to show auto complete drop down. I want to set selected value for edit. I tried following code which works for normal select option but not working for chosen select
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list))
{
foreach($list as $d)
{
?>
<option value="<?php echo $d->id; ?><?php if($d->id == 2) { echo "selected"; } ?>"><?php echo $d->name; ?></option>
<?php } } ?>
</select>
You are putting your selected inside your value attribute, you need to write it after :
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list)) {
foreach($list as $d) {
?>
<option value="<?php echo $d->id; ?>"<?php if($d->id == 2) { echo " selected"; } ?>><?php echo $d->name; ?></option>
<?php } } ?>
</select>
Building on #roberto06's answer, the following should be a bit cleaner to look at.
BTW, you really should consider using a template engine.
<select class="chosen-select">
<option value=""></option>
<?php if (!empty($list)): ?>
<?php foreach ($list as $d): ?>
<option value="<?php echo $d->id; ?>" <?php echo ($d->id == 2) ? "selected" : "">
<?php echo $d->name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>
Here I am listing all cars.customers want to compare car so they will select from this drop down. A person can select multiple cars. At the first time he is selecting 'Audi' and Saab' I will store it into data base next if he came I need to populate Saab and audi as select how I can do this using php
<select name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Here is my code
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
?> <option value="<?php echo($entry['ID']); ?>" ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
Following code I tried $resources contain select cars
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
if($resources->num_rows() >0)
{
foreach($resources->result_array() as $car):
if($entry['ID'] == $employee['car_id'])
{
$select = 'selected="selected"';
}
else
{
$select = '';
}
endforeach;
}
?> <option value="<?php echo($entry['ID']); ?>" <?php echo $select;?> ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
but it showing error
Here, try something like this, and see if it works:
Here is the controller:
<?php
function something(){
$data = array();
$data['cars'] = $this->some_model->some_function_to_return_cars_array();
$data['selected'] = $some_array_of_selected_cars();
$this->load->view('some_view', $data);
}
?>
And this is the view:
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<option value="">Select:</option>
<?php
foreach( $cars as $key => $val ){
?>
<option value="<?php echo $val['some_id'] ?>"
<?php
if( in_array( $val['some_id'], $selected ) ) echo ' selected';
?>
><?php echo $val['some_name'] ?></option>
<?php
}
?>
</select>
I am creating a like this:
<select name="id" id="id">
<OPTION VALUE="null">Select<OPTION>
<? foreach ($genres as $row){ ?>
<OPTION VALUE="<? echo $row->id; ?>"><? echo utf8_encode($row->name); ?></OPTION>
<?
}
?>
</select>
I am using the $_GET to check for a value on the URL and then just take it and with that value I have to pre-select the option in the select menu, any ideas how to do this? I'm unsure of how to do it via javascript (or even if it would be more complicated).
just use
selected="selected"
on your pre-selected option
<select name="id" id="id">
<OPTION VALUE="null">Select<OPTION>
<? foreach ($genres as $row){ ?>
$selected = $_GET['your_var'] == $row->id ? 'selected="selected"' : false;
<OPTION <? echo $selected; ?> VALUE="<? echo $row->id; ?>"><? echo utf8_encode($row->name); ?></OPTION>
<?
}
?>
</select>