I have a form which contains a dropdown list of countries generated from database. The values are stored in database. There is an option in which user can view or update the valuesinserted. For updating all the form values get fetched from database. What i require is that when form is loaded for updating the selected option of the country dropdown must be that stored in the database.
For eg: if from the following dropdown option2 is selected and inserted into database.
Dropdown: |option1|<selected>
|option2|
|option3|
during update it should be like this
Dropdown: |option1|
|option2|<selected>
|option3|
Here is the code i tried.
$selected = $list["country_country_name"];
<tr><td>Country</td><td><select onchange="getCountry(this.value);" name="country" id="country" ><?php foreach( $query as $qry ) {
print '<option value="'.$qry["country_country_name"].'"';
if( $qry["country_country_name"] == $selected ) print'selected';
print '>'.$qry["country_country_name"].'</option>'."\n";} ?>
</select></td></tr>
<select id="list" name="list">
<option value=""> Please Select </option>
<?
$list = array('1',
'2',
'3',
'4',
'5',
'6');
while ($L = array_shift($list)) {
?>
<option value="<?=$L?>" <? if($selected == $L){ echo 'selected="selected"'; }?> >
<?= $L ?>
</option>
<?
}
?>
</select>
you can simple get the selected option with this:
$("#list").val();
try please.
$selected = $list["country_country_name"];
<tr><td>Country</td>
<td>
<select onchange="getCountry(this.value);" name="country" id="country" >
<?php foreach( $query as $qry ) {
$sel = '';
if( $qry["country_country_name"] == $selected )
$sel = 'selected="selected"';
echo '<option value="'.$qry["country_country_name"].'" '.$sel.'>'.$qry["country_country_name"].'</option>'."\n";
} ?>
</select>
<?php echo form_error('country'); ?>
</td>
</tr>
When your option tag's value attribute contains the same value as the option's text, the value attribute is not necessary -- so omit it.
Below shows an inline condition statement.
<tr>
<td>Country</td>
<td><select onchange="getCountry(this.value);" name="country" id="country">
<?php
foreach ($query as $row) {
echo "<option" ,
($row["country_country_name"] == $list['country_country_name'] ? ' selected' : '') ,
"{$row['country_country_name']}</option>\n";
}
?>
</select></td>
</tr>
Related
Well i have a serious problem on updating a form, it only takes the selected value on user action in the select option menu but when the user updates the data without changing the select value it returns the default database value which is set to 0
<select class="selectpicker" name="socio_demo_type" id="socio_demo_type">
<?php
$query_sd = "SELECT * FROM insertion WHERE id_insertion = ".$insertion["id"];
$result_sd = mysql_query($query_sd);
$type = mysql_fetch_array($result_sd);
$val = $type["ciblage_socio_demo_type"];
?>
<option value="0" <?php if($val == 0) echo 'selected' ?> >Pack affinitaire </option>
<option value="1" <?php if($val == 1) echo 'selected' ?> >Achat ciblé (loggué)`enter code here`</option>
</select>
Pass the connection sting as well and use my sqli statements.
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
<select class="selectpicker" name="socio_demo_type" id="socio_demo_type">
<?php
$query_sd = "SELECT * FROM insertion WHERE id_insertion = "$insertion["id"];
$result_sd = mysqli_query($con , $query_sd);
$type = mysqli_fetch_array($result_sd);
$val = $type["ciblage_socio_demo_type"];
?>
<option value="0" <?php if($val == 0) echo 'selected' ?> > Option2 </option>
<option value="1" <?php if($val == 1) echo 'selected' ?> > Option1 </option>
</select>
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;?>
How can I select the option based on the value from db with php .For example;
<select>
<option value="1">Val1</option>
<option value="2">Val2</option>
<option value="3">Val3</option>
<option value="4">Val4</option>
<option value="5">Val5</option>
</select>
when value=3 change
<option value="3" selected="selected">Val3</option>
How can I do this with php&mysql?
You need something like
<?
$values = array(); // array from DB
$selectedKey = 10; // some key
?>
<select>
<? foreach ($values as $key => $value) { ?>
<option value="<?=$key?>" <?= $key==$selectedKey ? 'selected' : ''?>> <?=$value?> </option>
<? } ?>
</select>
Hope This Helps
<select>
<?php
$value = 3; // Desired Value
$sql ='' ;// sql to get values from mysql
$res = mysql_query($sql);
while($row=mysql_fetch_array($res))
{?>
<option value="<?php echo $row['id'];?>" <?phh if($row['id']==$value)echo "selected='selected'" ; ?> ><?php echo $row['name']; ?></option>
<?php }
?>
</select>
In the loop that prints out the possible values, compare each value to the value acquired from the DB. If they match, add the selected attribute.
First give the select tag a name.
<select name="name">
Get the table rows through a mysql_query into $row_set array
If say the value to be searched for is $search
while($row = mysql_fetch_array($row_set))
if($row['column_name']==$search) echo "<option value='{$row['value']}' selected='selected'>{$row['name']}</option>";
I have stored value from drop down list to database. I want to echo the same value to be displayed in that drop down list in my edit form. how can I achieve that in php?
Region
<select name="stf_region">
<option>Select</option>
<option value="1">MDU</option>
<option value="2">TMM</option>
</select>
i have stored in database using value of selection
but i dont know to display that value in same drop down
Use something like this:
<?
$sql = "SELECT id, description FROM dropDownTable";
$rs = mysql_query($sql);
?>
<select name="dropDown">
<option value="-1">Please select...</option>
<? while ($obj = mysql_fetch_object($rs)) { ?>
<option value="<?= $obj->id; ?>" <? if ($data['downDown'] == $obj->id) echo "SELECTED"; ?>>
<?= $obj->description; ?>
</option>
<? } ?>
</select>
Please note $data needs to be set as an associative array containing attributes of the entity that is being edited. This code is flexible because in the case of a form where a user may have submitted an incomplete form $data could be set to the $_POST variable and so all entered fields can be included without the user needing to re-specify fields they previously filled in. This basically means your form template, inserting an entry and editing an entry can be the same!
Well you could do like this
$query = "select id, label from lookup_table";
$result = mysql_query($query);
$html = "<select name='yourname'><option value="">Please select...</option>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$html .= "<option value='$row[id]'>$row[label]</option>";
}
$html = "</select>";
echo ($html);//Display the select in the page
If you're able to get db data into array, you can work with them like this:
<?php
$options = array('label 1' => 'value 1', 'label 2' => 'value 2');
echo "<select name=somename>";
foreach($options as $key => $value){
echo "<option value=" . $value . ">" . $key . "</option>";
}
echo "</select>";
?>
i used the COOKIE to match the value that should be selected when it comes to edit the form.
<?php
while($result_row=mysql_fetch_array($result)){
if ( $_COOKIE['MY_COOKIE'] == $result_row[importance_level_id )
{
echo "<option SELECTED=\"SELECTED\" value=$result_row[importance_level_id]>$result_row[importance_level]</option>";
}
else
{
echo "<option value=$result_row[importance_level_id]>$result_row[importance_level]</option>";
}
?>
Assuming you are ok with selecting the value out of the database into a PHP variable (let's say $region) I think you are just after
Region
<select name="stf_region">
<option>Select</option>
<option value="1"<?php echo ($region == '1') ? ' selected="selected"' : ''; ?>>MDU</option>
<option value="2"<?php echo ($region == '2') ? ' selected="selected"' : ''; ?>>TMM</option>
</select>
This is the most concise answer to the question that I think you are asking. However this is a very specific answer and assumes that your dropdown values are hard-coded and won't really be changing.
If you want a more flexible setup that retrieves the values of the dropdown from the database you are looking for something more along the lines of what has been suggested by Gordon Murray Dent above
<div class="form-group has-success col-md-6">
<label class="control-label" for="state-success">Select Buyer</label>
<select id="state-success" class="form-control ">
<?php
$sql="SELECT full_name FROM `new_customer`";
$data=mysqli_query($dbcon,$sql);
?>
<option>Select byer...</option>
<?php while($row1=mysqli_fetch_array($data)){?>
<option value="<?php echo $row1['full_name'];?>"><?php echo $row1['full_name'];?></option>
<?php } ?>
</select>
This question was asked already, but my question is very simple.
In the my account page, I have the employee country in a dropdown.
How to select a value in the combo, when in edit mode?
Let's assume you have the user's country in $user_country and the list of all countries in $all_countries array:
<select id="country">
<?php
foreach ( $all_countries as $country ):
$selected = "";
if ( $country == $user_country )
$selected = "selected";
?>
<option value="<?php echo $country; ?>"
selected="<?php echo $selected; ?>">
<?php echo $country; ?>
</option>
<?php
endforeach; ?>
</select>
should work.
An option tag will be the default for a select list when the selected attribute is set. In the following code option 2 will show up as the current selected option when the page loads:
<select>
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
</select>
To achieve this in your PHP code conditionally display the selected attribute on your options against what the current value is:
<option value="1"<?php if($user['country'] == '1') { ?> selected="selected"<?php } ?>>1</option>
<option value="2"<?php if($user['country'] == '2') { ?> selected="selected"<?php } ?>>2</option>
<option value="3"<?php if($user['country'] == '3') { ?> selected="selected"<?php } ?>>3</option>
function p_edit_combo($cCurstatus,$h_code_default,$h_name=NULL){
<select name="<?php echo $cCurstatus;?>" id="<?php echo $cCurstatus;?>" class="main_form_select">
<option value="">Select</option>
<?php
$sql_h = "SELECT h_code,h_name FROM med_hl WHERE status = 1";
$sql_h_result = mysql_query($sql_h);
while($row=mysql_fetch_array($sql_h_result)){
$h_code = $row['h_code'];
$h_name = $row['h_name'];
?>
<option <?php if($h_code_default==$h_code){ ?> selected="selected" <?php }?> value='<?php echo $h_code; ?>' >
<?php echo $h_code."|".$h_name; ?>
</option>
<?php } ?>
</select>
<?php
}
**i have two table
" users" colmns(fname,lname,...as on ohther_infomation,hobbies datatype(int))
"info" columns (id (primary_key),hobbies(varchar 200)); in which i stored for hobbies name
In my case i am storing values in from (1,2,3,4) in hobbies (int) filled of users table which i matached them through join after time of fetch them,
in my info table i stored hobbies by their name (reading, writing,playing,gyming)
$row has our users selected hobbies (int)
$rows has list of our hobbies(varchar)
edit.php i need Dropdown value selected :==== And i am Doing Like this :--- (100% Working)**
<div class="form-control">
<label for="hobbies">Hobbies</label>
<select name="hobbies">
<?php
$query = "SELECT * FROM info";
$results = mysqli_query($connect, $query);
while ($rows = mysqli_fetch_array($results)) {
?>
<option <?php if ($rows['id'] == $row['hobbies']) { ?> selected="selected" <?php } ?> value='<?php echo $rows['id']; ?>'>
<?php echo $rows['hobbies']; ?>
</option>
<?php
}
?>
</select>
<span class="text-danger"><?php if (isset($err_hobbies)) echo $err_hobbies; ?></span>
</div>