I have HTML form which have select options like below
<div class="form-group">
<label>Select your occupation:</label>
<select class="form-control" name = "occupation" required>
<?php if ($row[occupation] == 0){
}?>
<option value="0">Parent</option>
<option value="1">Teacher</option>
</select>
</div>
I want
option selected called Parent if ($row[occupation] == 0) else want
show Teacher as selected
. I am new in PHP and does not know how I can I do it. Let me know if someone can help me for do it.
Thanks
You have to echo selected='selected' based on the value of $row['occupation'], like this:
<div class="form-group">
<label>Select your occupation:</label>
<select class="form-control" name = "occupation" required>
<option value="0"<?php if($row['occupation'] == 0){ echo " selected='selected'"; } ?>>Parent</option>
<option value="1"<?php if($row['occupation'] == 1){ echo " selected='selected'"; } ?>>Teacher</option>
</select>
</div>
You can use this one.
<div class="form-group">
<label>Select your occupation:</label>
<select class="form-control" name="occupation" required>
<option value="0" <?php if ($row['occupation'] == 0) { echo 'selected'; } ?>>Parent</option>
<option value="1" <?php if ($row['occupation'] == 1) { echo 'selected'; } ?>>Teacher</option>
</select>
</div>
Related
I have a form with dropdown boxes, a radio buttons, and a few text fields. I know how to keep the text field values after form submit, but I would like to keep the dropdown value/data after submit. I am posting to the same page. here is the code below
<div class="form-group row">
<label class="col-sm-3 col-form-label">{{trans('words.shows_text')}}*</label>
<div class="col-sm-8">
<select class="form-control select2" name="series" id="episode_series_id">
<option value="">{{trans('words.select_show')}}</option>
#foreach($series_list as $series_data)
<option value="{{$series_data->id}}" #if(isset($episode_info->id) && $series_data->id==$episode_info->episode_series_id) selected #endif>{{$series_data->series_name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-3 col-form-label">{{trans('words.seasons_text')}}*</label>
<div class="col-sm-8">
<select class="form-control select2" name="season" id="episode_season_id">
<option value="">Select Season</option>
#if(isset($episode_info->id))
#foreach($season_list as $i => $season_data)
<option value="{{ $season_data->id }}" #if($season_data->id==$episode_info->episode_season_id) selected #endif>{{ $season_data->season_name }}</option>
#endforeach
#endif
</select>
</div>
</div>
You can use the session variables. So, first start the session:
session_start();
When the form is submitted, store the selected values in session variables:
$_SESSION['radio_value'] = $_POST['radio'];
$_SESSION['checkbox_values'] = $_POST['checkbox'];
$_SESSION['selected_option'] = $_POST['dropdown'];
And finally, use the session variables to set the checked attribute of the appropriate input elements:
<input type="radio" name="radio" value="option1" <?php if ($_SESSION['radio_value'] == 'option1') { echo 'checked'; } ?>>
<input type="checkbox" name="checkbox[]" value="option1" <?php if (in_array('option1', $_SESSION['checkbox_values'])) { echo 'checked'; } ?>>
<select name="dropdown">
<option value="option1" <?php if ($_SESSION['selected_option'] == "option1") echo "selected"; ?>>Option 1</option>
<option value="option2" <?php if ($_SESSION['selected_option'] == "option2") echo "selected"; ?>>Option 2</option>
<option value="option3" <?php if ($_SESSION['selected_option'] == "option3") echo "selected"; ?>>Option 3</option>
</select>
Of course, you will need to make some extra "ifs" in your code.
Edit.blade when i click edit i want the drop-down-list to show the data which i have save from the database.
Controller:
public function edit($id)
{
//
$igcse = IGCSE_Student::findOrFail($id);
$students = Student::all();
$levels = Level::all();
$classes = Classes::all();
return view('igcse.edit', compact('igcse', 'students','levels','classes'));
}
Edit.Blade
<div class="form-group">
<label>Status</label>
<select style="width: 200px" class="form-control" id="status" name="status" required>
<option value="0" >Please Select</option>
<option value="ACTIVE">ACTIVE</option>
<option value="WITHDRAW">WITHDRAW</option>
<option value="GRADUATE">GRADUATE</option>
</select>
</div>
Note:
In the drop-down-list i want to display the current status of the student from the database.
Your edit structure should be like this
<div class="form-group">
<label>Status</label>
<select style="width: 200px" class="form-control" id="status" name="status" required>
<option value="0" #if($igcse->status=='0') selected #endif>Please Select</option>
<option value="ACTIVE" #if($igcse->status=='ACTIVE') selected #endif>ACTIVE</option>
<option value="WITHDRAW" #if($igcse->status=='WITHDRAW') selected #endif>WITHDRAW</option>'
<option value="GRADUATE" #if($igcse->status=='GRADUATE') selected #endif>GRADUATE</option>
</select>
</div>
edit blade file should be like this
<div class="form-group">
<label>Status</label>
<select style="width: 200px" class="form-control" id="status" name="status" required>
<option value="0" >Please Select</option>
<option value="ACTIVE" {{$igcse->status=='ACTIVE' ? 'selected' : '' }}>ACTIVE</option>
<option value="WITHDRAW" {{$igcse->status=='WITHDRAW' ? 'selected' : '' }}>WITHDRAW</option>
<option value="GRADUATE" {{$igcse->status=='GRADUATE' ? 'selected' : '' }}>GRADUATE</option>
</select>
</div>
Assuming you are reffering to students and you want the select tag automatically select the option based from data on database.
<div class="form-group">
<label>Status</label>
<select style="width: 200px" class="form-control" id="status" name="status" required>
<option value="0" >Please Select</option>
<option value="ACTIVE" #php($igcse->status == 'ACTIVE') ? echo 'selected' : "" #endphp>ACTIVE</option>
<option value="WITHDRAW" #php($igcse->status == 'WITHDRAW') ? echo 'selected' : "" #endphp>WITHDRAW</option>
<option value="GRADUATE" #php($igcse->status == 'GRADUATE') ? echo 'selected' : "" #endphp>GRADUATE</option>
</select>
</div>
When I select the all data of a product by it's product_id. and load product data in a form for an update. but here I cannot set selected drop-down list value in this case.
here, is my view
<form action="<?php //echo base_url().'admin_product/pro_update/'?>" method="post" class="form-horizontal">
<div class="control-group">
<label class="control-label">Title :</label>
<div class="controls">
<input type="text" class="span5 m-wrap" placeholder="Product title" name="title" value="<?php echo $item[0]['p_title']?>" />
<h6 style="color:red"><?php echo form_error('title')?></h6>
</div>
</div>
<div class="control-group">
<label class="control-label">Category :</label>
<div class="controls">
<select name="cat" id="cat">
<option value="">--Select Category--</option>
<?php foreach($cat as $c):?>
<option value="<?php echo $c->cat_id?>"><?php echo $c->cat_name;?>//selected set value here</option>
<?php endforeach;?>
</select>
<h6 style="color:red"><?php echo form_error('cat')?></h6>
</div>
</div>
here is my controller code,
public function pro_edit($id)
{
$p['item']=$this->ap->product_update($id);
$p['cat']=$this->ap->up_cat();
$p['color']=$this->ap->up_color();
$this->load->view('layouts/header',$p);
$this->load->view('admin/product_update',$p);
$this->load->view('layouts/footer',$p);
}
and model code,
public function product_update($id)
{
$this->db->select('p_title,p_description,p_category,p_stock_quantity,p_pprice,p_sprice,p_size_variant,cat_name,v_color,v_size,p_id');
$this->db->from('tbl_product');
$this->db->join('tbl_category','tbl_product.p_category=tbl_category.cat_id');
$this->db->join('tbl_variant','tbl_product.p_color_variant=tbl_variant.v_id');
$this->db->where('p_id',$id);
$query=$this->db->get();
return $query->result_array();
}
Assumptions
p_category is your category id
Code
<select name="cat" id="cat">
<option value="">--Select Category--</option>
<?php
foreach($cat as $c):?>
<option value="<?php echo $c->cat_id?>" <?php echo ($item[0]['p_category'] == $c->cat_id) ? 'selected' : '' ?>>
<?php echo $c->cat_name;?>
</option>
<?php
endforeach;
?>
</select>
Explain
Q - What this if do in above <?php echo ($item[0]['p_category'] == $c->cat_id) ? 'selected' : '' ?>
A - Assume $item[0]['p_category'] has value 2 so when ever $c->cat_id equesl to that value its print selected keyword
Ex:
<select>
<option value="1">aa</option>
<option value="2" selected>bb</option>
<option value="3">cc</option>
<option value="4">dd</option>
</select>
I have built a form that has text fields, text areas, radio buttons, and drop downs, and I've created a query to pull the data from the database at the appropriate row, but can't figure out how to program the drop down to reflect the choice that's in the mysql row. Here are a sample of the fields I've been able to program correctly:
<label for="contactname">Contact Name:</label>
<input type="text" name="contactname" value="<?php echo $query['contactname']; ?>"
<label for="delay">Delay:</label>
<input id="Yes" type="radio" name="delay" value="Y" <?php if($query['delay']==='Y') echo 'checked'; ?> >Yes
<input id="No" type="radio" name="delay" value="N" <?php if($query['delay']==='N') echo 'checked'; ?> >No
<label for="notes">Notes: </label>
<textarea name="notes" id="notes"><?php echo $query['notes']; ?></textarea>
These have all worked in pulling data from the mysql database correctly, but I'm trying to figure out how to program the drop downs so that when this form is pulled, the selection populates/displays the selected value when the form is pulled:
<label for="day">Select Option:</label>
<select name="day">
<option value="0">Monday </option>
<option value="1">Tuesday </option>
<option value="2">Wednesday </option>
<option value="3">Thursday </option>
<option value="4">Friday </option>
</select>
Thanks for your help!
You need to append " selected" to the option you want pre-selected in the dropdown.
<label for="day">Select Option:</label>
<select name="day">
<option value="0"<?php if ($query['day'] == 0) echo ' selected="selected"'; ?>>Monday</option>
<option value="1"<?php if ($query['day'] == 1) echo ' selected="selected"'; ?>>Tuesday</option>
<option value="2"<?php if ($query['day'] == 2) echo ' selected="selected"'; ?>>Wednesday </option>
<option value="3"<?php if ($query['day'] == 3) echo ' selected="selected"'; ?>>Thursday</option>
<option value="4"<?php if ($query['day'] == 4) echo ' selected="selected"'; ?>>Friday</option>
</select>
try this example:
//array as an example
$your_db_array = array ('value1','value2','value3');
// Make the pull-down menu:
echo '<select name="pull_down">';
foreach ($your_db_array as $key => $value) {
echo "<option value=\"$key\"";
// Check for stickyness:
if (isset($value_from_DB['value']) && ($value_from_DB['value'] == $key) ){
echo ' selected="selected"';}
echo ">$value</option></select>";
}
I want to update my form when I click edit button then all info is showing correctly but status value is showing all time same open option. I dont know why it is showing same open status and my currently status is done but it is showing all time open please help me to fix this issue thanks
this is my form code username is showing correctly but status is not showing correct
<p><label class="field" for="username">UserName:</label>
<input name="username" type="text" id="username" value="<?php echo $username; ?>" size="50" />
</p>
<p>
<label class="field" for="Status">Status</label>
<select name="status" id="status" value="<?php echo $status; ?>" >
<option value="open">Open</option>
<option value="done">Done</option>
<option value="pending">Pending</option>
<option value="working">Working</option>
</select>
</p>
Use selected attribute.
<select name="status" id="status">
<option value="open" <?php if($status=="open") { echo "selected"; } ?> >Open</option>
<option value="done" <?php if($status=="done") { echo "selected"; } ?> >Done</option>
<option value="pending" <?php if($status=="pending") { echo "selected"; } ?> >Pending</option>
<option value="working" <?php if($status=="working") { echo "selected"; } ?> >Working</option>
</select>
This is wrong way, correct way is to use like this,
<select name="status">
<?php
$options = array("open","done","pending","working");
$selected = "done";
foreach($options as $option){
if($selected==$option){
echo '<option value="'.$option.'" selected="selected">'.ucfirst($option).'</option>';
}else{
echo '<option value="'.$option.'">'.ucfirst($option).'</option>';
}
}
?>
</select>
If you really want to embed the status in the options tag, you should use the 'selected' attribute for selection of the tag options. Here is your modified code with correct handling:-
<p><label class="field" for="username">UserName:</label>
<input name="username" type="text" id="username" value="<?php echo $username;?>" size="50" />
</p>
<p>
<label class="field" for="Status">Status</label>
<select name="status" id="status" >
<option value="open" <?php echo $status == 'open' ? 'selected' : ''; ?>>Open</option>
<option value="done" <?php echo $status == 'done' ? 'selected' : '' ;?>>Done</option>
<option value="pending" <?php echo $status == 'pending' ? 'selected' : '' ; ?>>Pending</option>
<option value="working" <?php echo $status == 'working' ? 'selected' : '' ; ?>>Working</option>
</select>
</p>
You need selected tag. Modify your form like this
<select name="status" id="status"> <!-- value removed, there's no use of it here -->
<option value="open" <?php if ($status=="open") {echo "selected"}?>>Open</option>
<option value="done" <?php if ($status=="done") {echo "selected"}?>>Done</option>
....
</select>
Use selected attribute for option.
Change your code as
<select name="status" id="status">
<option value="open" <?php selected($status, 'open'); ?>>Open</option>
<option value="done" <?php selected($status, 'done'); ?>>Done</option>
<option value="pending" <?php selected($status, 'pending'); ?>>Pending</option>
<option value="working" <?php selected($status, 'working'); ?>>Working</option>
</select>
<?php
function selected( $selected_value, $value ) {
if( $selected_value === $value ) {
echo 'selected=true';
}
}
?>