Keep form values after submit - php

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.

Related

Pass value of html to PHP variable

I'm having some trouble passing the value of some HTML elements to PHP variable. I have a PHP web app where I give the users some drop down option. What I want to do is after the user has selected the option I give than to click on a button and make some calculations based on his/her selection.
I have tried using the POST and GET method but because I'm new to php I probably did it wrong. I also try with ids but no results.
Here is the HTML code
The 1st dropdown menu :
<div>
<select id="kleidwma" onchange="kleidwmata_change(this.value);kwdikos();ypsos();"
class="form-control selectpicker kleidwmata" data-size="10" data-
style="btn-white" name="guard_kleidwmata" required >
<option value="" > </option>
<option value="12">12 MS</option>
<option value="14">14 KETE</option>
<option value="15">15 ARRIKTON</option>
<option value="16">16 KETE</option>
<option value="22">22 KETE</option>
</select>
</div>
The 2nd dropdown menu :
<div>
<select id="sigkratisi" onchange="kwdikos()" name="sigkratisi"
class="form-control sigkratisi" required>
<option value="" ></option>
<option value="metalliki" >Μεταλλική</option>
<option value="xilogonia" >Ξυλογωνία</option>
</select>
</div>
Here is the button to be clicked after the selection has been made :
<div class="row" style="margin-top:20px; margin-left: 235px;">
<div class="col-md-7">
<input type="hidden" name="tmp_calculate" value="pending" />
<button onclick="calculate()" type="button" class="btn btn-success" >
<?php echo "Υπολογισμός";?></button>
</div>
</div>
And finally here is the function that is called :
function calculate()
{
$var1 = sigkratisi;
$var2 = kleidwma;
"code to be executed base on the var1 and var2"
}
I hope it's clear enough.
Thanks in advance for your time
you can use Forms
<form name="f" action="" method="POST">
<div>
<select id="kleidwma" name="variable"
class="form-control selectpicker kleidwmata" data-size="10" data-
style="btn-white" name="guard_kleidwmata" required >
<option value="" > </option>
<option value="12">12 MS</option>
<option value="14">14 KETE</option>
<option value="15">15 ARRIKTON</option>
<option value="16">16 KETE</option>
<option value="22">22 KETE</option>
</select>
<input type="submit" value="submit" />
</div>
</form>
in PHP
<?php
if(isset($_POST['variable']))
{
echo $_POST['variable'];
}
?>

set option selected using php value

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>

Return Value of Drop down list

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>

select previously selected dropdown value on form self submit

I have a 'this year' dropdown with a form attached. I then select one of the years, like say 2016. When I click the submit button I want the page to be reloaded while the value in the dropdown stays at 2016 (the one that was previously selected).
How can I do that? Here's my form:
<form class="form" method="POST" id="myID" name="myName">
<label id="year" name="year">YEARS</label>
<select name="year" id="year" class="form-control">
<option value="0" selected disabled="">CHOOSE YEAR</option>
<?php foreach($year as $rows){?>
<option value="<?php echo $rows->years?>"><?php echo $rows->years?>
</option>
<?php }?>
</select>
<button id="filter_button" style="margin-top: 26px" name="filter_button"
type="submit" class="for btn btn-info">Show</button>
</form>
You should use the value from $_POST to set the selected as default. Below is the updated code:
<form class="form" method="POST" id="myID" name="myName">
<label id="year" name="year">YEARS</label>
<select name="year" id="year" class="form-control">
<option value="0" selected disabled="">CHOOSE YEAR</option>
<?php foreach($year as $rows){
$isSelected = (isset($_POST['year']) && ($_POST['year'] == $rows->years)) ? 'selected' : '';
?>
<option <?php echo $isSelected; ?> value="<?php echo $rows->years?>"><?php echo $rows->years?>
</option>
<?php }?>
</select>
<button id="filter_button" style="margin-top: 26px" name="filter_button"
type="submit" class="for btn btn-info">Show</button>
</form>

How to show selected value from database and show in dropdownlist using codeigniter and bootstrap

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>

Categories