I have a Dorpdown menu and when I select a value there is a table which chnages there entries regarding to this value. Out of the table I can open every single entry in a new page. I want to make a "Back" Button and when I go back to the page there should be the value selected whch I selected before.
Here is my Dropdown Code:
<div class="ccol-l-4 col-m-12">
<form method="post" accept-charset="utf-8" action="<?php echo site_url("workpackage"); ?>">
<?php echo form_dropdown('wptypes', $wptypes, set_value('wptypes'), 'id="wptypes" class="form-select" onchange="this.form.submit()"'); ?>
</form>
</div>
Thank you for your answers :)
You can use $.cookie
to get and set your selected item value into.
<select id="yourid">
</select>
$("yourid").on("change", function(){
$.cookie("selectedItem", $(this).val())
})
// when you return to page
var val = $.cookie("selectedItem");
if(val != null)
{
$('#yourid option[value="' + val + '"]').attr('selected', 'selected');
}
You have to set the post value by adding a condition in view like,
<?php echo form_dropdown('wptypes', $wptypes,
// check if the post wptypes is set then make that option selected
(isset($_POST['wptypes']) ? $_POST['wptypes'] : ''),
'id="wptypes" class="form-select" onchange="this.form.submit()"'); ?>
Also, if the page is different then use localStorage to save the current option in it.
Remove onchange
from your html
<div class="ccol-l-4 col-m-12">
<form method="post" accept-charset="utf-8" action="<?php echo site_url("workpackage"); ?>">
<?php echo form_dropdown('wptypes', $wptypes, set_value('wptypes'), 'id="wptypes" class="form-select"'); ?>
</form>
</div>
Add below code in JS
$(function(){
// add on change event
$('#wptypes').on('change',function(){
if(this.value){ // if this value is not blank
// set value in localStorage
localStorage.setItem('wptypes',this.value);
// finally, form submits
this.form.submit();
}
});
// Code to make selected wptypes options
localStorage.getItem('wptypes') && // if value exists in localStorage
$('#wptypes').val(localStorage.getItem('wptypes')) // set it
});
Well, You have 3 options here:
The pattern is very simple that OnSelect dropdown get selected value, save those values somewhere which is accessible later by you. Check if that particulate storage has any kind of value, get those values from there & set back inside dropdown again..
I am telling you here: How we could use CodeIgniter Session + jQuery, to get back your dropdown value.
Your html or views:
<form method="post" accept-charset="utf-8" action="<?php echo site_url("workpackage"); ?>">
<input type="hidden" name="<?=$this->security->get_csrf_token_name()?>" value="<?=$this->security->get_csrf_hash()?>">
<div class="form-group">
<label for="name">Client</label>
<?php echo form_dropdown('wptypes', $wptypes, set_value('wptypes'), 'id="wptypes" class="form-select" onchange="this.form.submit()"'); ?>
</div>
</form>
<script type="text/javascript">
$(document).ready(function(){
// wptypes select box
var $wptypes = $('select#wptypes');
// check on load of wptypes value exist in session
var _prev_sel_val = "<?=$this->session->userdata('wptypes') ? $this->session->userdata('wptypes') : '';?>";
// if not empty, set value
if(_prev_sel_val){
$wptypes.val(_prev_sel_val);
}
});
</script>
Your Controller (Example):
<?php
// security first always....
(defined('BASEPATH') or exit('No direct script access allowed'));
/**
* Class Controller
*
* Class workpackage Controller to handle login & logout
*/
class Workpackage extends CI_controller
{
/**
* Class Constructor
*/
public function __construct()
{
// execute parent class constructor
parent::__construct();
}
/**
* workpackage, Default method to execute if method name missing
* @return [type] [description]
*/
public function index()
{
// only on POST Request
if ($this->input->post('wptypes')) {
// drop down value
$wptypes = array(
'wptypes' => $this->input->post('wptypes', TRUE)
);
// set in session
$this->session->set_userdata( $wptypes );
// your other stuff do..... here...
}
}
}
/* End of file workpackage.php */
/* Location: ./application/controllers/workpackage.php */