How to auto set the textbox based on dropdown selection? - php

How to auto set the textbox based on dropdown selection? I have a dropdown for position and textbox for department. What I would like to do is when "Guidance Faculty" is selected from dropdown, the textbox will automatically set to "Guidance" and it will become readonly and same in when "OSA Faculty" is selected from dropdown, the textbox will automatically set to "OSA" and it will become readonly. While if "Department Chair" and "Professor" is selected nothing will happen to textbox or it will set to "".
<div class="form-group">
<label for="position">Position</label>
<?php
echo form_open('signup');
$options = array(
'' => 'Select Position',
'Department Chair' => 'Department Chair',
'Professor' => 'Professor',
'Guidance Faculty' => 'Guidance Faculty',
'OSA Faculty' => 'OSA Faculty',
);
echo "<div class='drop_pos'>";
echo form_dropdown('position', $options, 'class="btn dropdown-toggle"', 'data-toggle="dropdown-menu"');
?>
<div class="text-danger"><?php echo form_error('position');?></div>
</div>
</div>
<div class="form-group">
<label for="department">Department</label>
<input class="form-control" name="department" placeholder="Department" type="text" value="<?php echo set_value('Department');?>"/>
<span class="text-danger"><?php echo form_error('department');?></span>
</div>

Try this:
<div class="form-group">
<label for="position">Position</label>
<?php
echo form_open('signup');
$options = array(
'' => 'Select Position',
'Department Chair' => 'Department Chair',
'Professor' => 'Professor',
'Guidance Faculty' => 'Guidance Faculty',
'OSA Faculty' => 'OSA Faculty',
);
echo "<div class='drop_pos'>";
echo form_dropdown('position', $options, 'class="btn dropdown-toggle"', 'data-toggle="dropdown-menu" onChange="javascript:document.getElementsByName(\'department\').value=this.value;"');
?>
<div class="text-danger"><?php echo form_error('position');?></div>
</div>
</div>
<div class="form-group">
<label for="department">Department</label>
<input class="form-control" name="department" placeholder="Department" type="text" value="<?php echo set_value('Department');?>"/>
<span class="text-danger"><?php echo form_error('department');?></span>
</div>

Make use of JS like below.
First set dropdown box like this...
echo form_dropdown('position', $options, array('id'=>'pos','class'=>'btn dropdown-toggle','data-toggle'=>'dropdown-menu','onchange'=>'changefuc();'));
Then add JS
<script type="text/javascript">
function changefuc(){
var pos = document.querySelector('#pos');
var dept = document.querySelector('[name="department"]');
pos.addEventListener('change',function(){
dept.value = this.value;
});
}
</script>
UPDATE
See demo
var pos = document.querySelector('#pos');
var dept = document.querySelector('[name="department"]');
pos.addEventListener('change',function(){
dept.value = this.value;
});
<select id="pos">
<option value="" selected >Select Position</option>
<option value="Department Chair">Department Chair</option>
<option value="Professor">Professor</option>
<option value="Guidance Faculty">Guidance Faculty</option>
<option value="OSA Faculty">OSA Faculty</option>
</select>
<input class="form-control" name="department" placeholder="Department" type="text"/>

in jquery something like this:
jQuery("select[name='position']").on("change", function() {
// value of selected option
var selectedPosition = $(this).val();
if (selectedPosition) {
// the inputbox
jQuery("input[name='department']").val(selectedPosition).attr('readonly', true);
} else {
jQuery("input[name='department']").val('').attr('readonly', false);
}
});

Related

Price range filter using CodeIgniter4

I am trying to create a search with multiple filters, one of the filters is a budget or price filter and I am using dropdown instead of creating a price range bar.
In other words, my budget filters are:-
0-2500000
250000-500000
500000-10000000
10000000-25000000
25000000-50000000
50000000 and above
Here is my code and I need help modifying it. The function in Controller and SQL Query in the model only takes a single budget (price) value and displays the result, instead of taking the budget (price) range and showing the result which comes under that budget (price) range.
What I want to achieve is that when the user selects the budget (price) range in the search form, it should display properties that fall under the selected budget range or price range.
HTML Form
<div class="search__area-inner">
<?= form_open('/home/search_residential'); ?>
<div class="row">
<div class="col-12"><?= csrf_field(); ?></div>
<div class="col-6 col-lg-4 col-md-4">
<div class="form-group">
<select name="budget[]" class="wide select_option">
<option data-display="Select Budget">Select Budget</option>
<option value="0-2500000">less than 25 lacs</option>
<option value="250000-500000">25 to 50 lacs</option>
<option value="500000-10000000">50 lacs to 1 cr</option>
<option value="10000000-25000000">1 cr to 2.5 cr</option>
<option value="25000000-50000000">2.5 cr to 5 cr</option>
<option value="50000000">5 cr +</option>
</select>
</div>
</div>
<div class="col-6 col-lg-4 col-md-4">
<div class="form-group">
<select name="type" class="wide select_option">
<option data-display="Select Property Type">Select Property Type</option>
<option value="Apartments">Apartments</option>
<option value="Bungalow">Bungalow</option>
<option value="Row House">Row House</option>
<option value="Villas">Villas</option>
</select>
</div>
</div>
<div class="col-6 col-lg-4 col-md-4">
<div class="form-group">
<input type="text" class="form-control" id="inputLocation" name="city" value="" placeholder="Enter City" required>
</div>
</div>
<br>
<div class="mx-auto">
<div class="form-group">
<button class="btn btn-primary text-uppercase btn-block"> search
<i class="fa fa-search ml-1"></i>
</button>
</div>
</div>
</div>
<?= form_close(); ?>
</div>
Controller code
public function search_residential() {
$data['getfooter'] = $this->homeModel->getFooterInfo();
$data['getad'] = $this->homeModel->getCarousel();
$data['pagelist'] = $this->pageModel->getPages();
$data['cities'] = $this->db->table('tblcity')->select('*')->get()->getResult();
$params = array(
'budget' => $this->request->getVar('budget'),
'type' => $this->request->getVar('type'),
'city' => $this->request->getVar('city'),
);
$data['search'] = $this->homeModel->getSearch($params);
return view('searchresidential', $data);
}
**Model code**
//Searching residential data
public function getSearch($params) {
$budget = $params['budget'];
$type = $params['type'];
$city = $params['city'];
$builder= $this->db->table('tblresidential');
$builder->select('*');
$builder->where('1 = 1');
if ($budget != '') {
$builder->where('budget', $budget);
}
if ($type != '') {
$builder->where('type', $type);
}
if ($city != '') {
$builder->where('city', $city);
}
return $builder->get()->getResult();
}
Instead of:❌
// ...
$builder->where('budget', $budget);
// ...
Use this:✅
// ...
foreach ($budget as $range) {
$builder->orWhere((function (string $range) {
$limits = array_map(
fn($limit) => floatval($limit),
explode("-", $range)
);
return match (count($limits)) {
1 => ['budget >=' => $limits[0]],
2 => ['budget >=' => $limits[0], 'budget <=' => $limits[1]],
default => ['1 =' => 1]
};
})($range));
}
// ...
Resource: $builder->where() : Associative array method

inserting multiple fields with same name as a single record into database

In my application there is a form which have a two select field , in which the last select is set to have a multiple selection. What I need is to insert the first pair of select field to be inserted as a single record into the database. When I try to do this using a for loop I am able to insert the record as a single row until and unless I select only one value for my 2nd select field. But If I select multiple fields for second select, the records isn't proper.
Also here user can add more divs by clicking ADD button
<div id="scope-brand-div">
<div class="col-md-6">
<div class="form-group">
<label for="scope">Project Scope</label>
<select name="scope[]" class="form-control scope-select">
<option value=""></option>
<option>A</option>
<option>B</option>
<option>C</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="brands_used">Brands Used</label>
<select multiple name="brands_used[]" class="form-control brand-select">
<option value=""></option>
<option>D</option>
<option>E</option>
<option>F</option>
</select>
</div>
</div>
</div>
php
$s = $this->input->post('scope');
$b = $this->input->post('brands_used');
foreach( $s as $key => $n ) {
$rows[] = $n.",".$b[$key];
}
print_r($rows);
SCENARIO 1
If I select single fields for both select, the output will be:
A, D
B, E
SCENARIO 2
If I select a multiple filed for the second select like A,(D,E) and B,F. Using the same loop above the result will be:
A, D
B, E
How can I insert the same fields If I select multiple fields for 2nd select field ad provided the user can add more divs same like this.
Edit 1
<div id="scope-brand-div">
<div class="col-md-6">
<div class="form-group">
<label for="scope"><?php echo $this->lang->line('xin_project_scope');?></label>
<select name="scope[]" class="form-control select-border-color border-warning scope-select" data-plugin="select_hrm" data-placeholder="<?php echo $this->lang->line('xin_project_scope');?>">
<option value=""></option>
<?php foreach($all_project_scope as $scope) { ?>
<option value="<?php echo $scope->scope_name; ?>"><?php echo $scope->scope_name; ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group brands-ajax">
<label for="brands_used"><?php echo $this->lang->line('xin_brands_used');?></label>
<select multiple name="brands_used[]" class="form-control select-border-color border-warning brand-select" data-plugin="select_hrm" data-placeholder="<?php echo $this->lang->line('xin_brands_used');?>">
<option value=""></option>
<?php foreach($all_brands as $brands) { ?>
<option value="<?php echo $brands->brand_name; ?>"><?php echo $brands->brand_name; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#add-new').click(function(){
var invoice_items = '<div id="scope-brand-div">'
+ '<div class="col-md-6">'
+'<div class="form-group">'
+ '<label for="scope"><?php echo $this->lang->line('xin_project_scope');?></label>'
+'<select name="scope[]" class="form-control scope-select" data-placeholder="<?php echo $this->lang->line('xin_project_scope');?>">'
+'<option value=""></option>'
<?php foreach($all_project_scope as $scope) { ?>
+'<option value="<?php echo $scope->scope_name; ?>"><?php echo $scope->scope_name; ?></option>'
<?php } ?>
+ '</select>'
+ '</div>'
+ '</div>'
+ '<div class="col-md-6">'
+ '<div class="form-group brands-ajax">'
+ '<label for="brands_used"><?php echo $this->lang->line('xin_brands_used');?></label>'
+'<select name="brands_used[]" class="form-control brand-select" data-placeholder="<?php echo $this->lang->line('xin_brands_used');?>">'
+'<option value=""></option>'
<?php foreach($all_brands as $brands) { ?>
+'<option value="<?php echo $brands->brand_name; ?>"><?php echo $brands->brand_name; ?></option>'
<?php } ?>
+'</select>'
+'</div>'
+ '</div>'
+'</div>'
$('#clone-parent').append(invoice_items).fadeIn(500);
});
});
</script>
<?php
public function add_project() {
$s = $this->input->post('scope');
$b = $this->input->post('brands_used');
foreach( $s as $key => $n ) {
$rows[] = $n.",".$b[$key];
//insert query
}
}
?>
Edit 2
echo var_export($this->input->post('brands_used'));
array (
0 =>
array (
0 => 'Opterna',
),
1 =>
array (
0 => 'Norden',
),
2 => 'Opterna',
)
echo var_export($this->input->post('scope'));
array (
0 => 'ACS',
1 => 'SMA TV',
)
About your html...
the for in your label only works properly if associated with the id attribute of your field.
<option> tags which have identical values in the text and in the value attribute can have their value attribute removed entirely -- it works exactly the same at submission time but removes bloat from your html.
I am appending a counter to the id and the clonable class to your outermost <div> to aid the javascript cloning process.
brands_used[][] needs to be brands_used[0][] to prevent unwanted incrementation of the first level index.
Rewrite:
<div id="scope-brand-div0" class="clonable">
<div class="col-md-6">
<div class="form-group">
<label for="scope0"><?php echo $this->lang->line('xin_project_scope'); ?></label>
<select id="scope0" name="scope[]" class="form-control select-border-color border-warning scope-select" data-plugin="select_hrm" data-placeholder="<?php echo $this->lang->line('xin_project_scope'); ?>">
<option></option>
<?php foreach ($all_project_scope as $scope) {
echo "<option>{$scope->scope_name}</option>";
} ?>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group brands-ajax">
<label for="brands_used0"><?php echo $this->lang->line('xin_brands_used'); ?></label>
<select multiple id="brands_used0" name="brands_used[0][]" class="form-control select-border-color border-warning brand-select" data-plugin="select_hrm" data-placeholder="<?php echo $this->lang->line('xin_brands_used'); ?>">
<option></option>
<?php foreach ($all_brands as $brands) {
echo "<option>{$brands->brand_name}</option>";
} ?>
</select>
</div>
</div>
</div>
As for your cloning javascript... I advise that you use clone() to clean up your script. Perhaps draw some inspiration from https://stackoverflow.com/a/8018242/2943403.
You will need to ensure that a "counter" is implemented. The original/hardcoded set of fields will have 0 as the counter; all additional sets of fields should replace all 0 values with the next incremental integer. This will keep all related fields in their related groups at submission time.
Specifically:
<div id="scope-brand-div0"> (if you actually need it) will become <div id="scope-brand-div1">
<label for="scope0"> will become <label for="scope1">
<select id="scope0"> will become <select id="scope1">
<label for="brands_used0"> will become <label for="brands_used1">
<select multiple id="brands_used0" name="brands_used[0][]" will become <select multiple id="brands_used1" name="brands_used[1][]"
The, of course, if the Add button is clicked a second time, all of the 1's need to become 2's.
As for processing your $_POST data, I expect to receive a structure like this: (don't stop tweaking your form until it delivers this most-appropriate structure)
$_POST = [
'scope' => [
'ACS',
'SMA TV'
],
'brands_used' => [
[
'Opterna',
'Norden'
],
[
'Opterna'
]
]
];
This means you can use the same first level keys to access related scope and brands_used values. As you traverse the data, scope data will be a string and brands_used will be an array.
To iterate:
// make an object-oriented mysqli connection ($mysqli)
$stmt = $mysqli->prepare("INSERT INTO tablensme (scope, brand_used) VALUES (?, ?)");
$stmt->bind_param('ss', $scope, $brand_used);
$posted = $this->input->post();
foreach ($posted['scope'] as $index => $scope) {
// use $scope string
foreach ($posted['brands_used'][$index] as $brand_used) {
// use $brand_used string
$stmt->execute();
}
}
Assuming you have a normalized db table structure and want to write one scope value and one brand_used value per table row, set up your prepared statement and bound variables prior to the loop, then call execute() from the inner loop.
This is 100% NOT TESTED, but the intended result from inserting the sample data would be the following new rows:
('ACS', 'Opterna'),
('ACS', 'Norden'),
('SMA TV', 'Opterna')

Removing options from subsequent select lists once they are selected

I am using a stats app that will attach players (obtained from data within a database) to particular positions. I want players to disappear from being selected in forthcoming positions if they have already been selected so that the same player cannot be put in two separate positions. Can anyone suggest what to do here
This is the code, which i have summarised
<div class="row">
<div class="col-sm-4">
<label>1. Goalkeeper</label>
</div>
<div class="col-sm-4">
<select class="form-control">
<option value="" disabled selected>Select player</option>
<?php
include('conn.php');
$selectplayer= "SELECT * from panel";
$playerresult=$conn->query($selectplayer);
if(!$playerresult){
echo $conn->error;
}
while ($select=$playerresult->fetch_assoc()){
$identify=$select['id'];
$name=$select['name'];
echo "<option>$identify. $name</option>";
}
?>
</select>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<label>2. Right corner back</label>
</div>
<div class="col-sm-4">
<select class="form-control">
<option value="" disabled selected>Select player</option>
<?php
include('conn.php');
$selectplayer= "SELECT * from panel";
$playerresult=$conn->query($selectplayer);
if(!$playerresult){
echo $conn->error;
}
while ($select=$playerresult->fetch_assoc()){
$identify=$select['id'];
$name=$select['name'];
echo "<option>$identify. $name</option>";
}
?>
</select>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<label>3. Full back</label>
</div>
<div class="col-sm-4">
<select class="form-control">
<option value="" disabled selected>Select player</option>
<?php
include('conn.php');
$selectplayer= "SELECT * from panel";
$playerresult=$conn->query($selectplayer);
if(!$playerresult){
echo $conn->error;
}
while ($select=$playerresult->fetch_assoc()){
$identify=$select['id'];
$name=$select['name'];
echo "<option>$identify. $name</option>";
}
?>
</select>
</div>
</div>
Create a datalist with your players, and JQuery to remove from it when selected one using onchange() on the input:
function resetPlayerList(pos) {
//Get player name
var player = $('input#' + pos).val();
//If player exists in datalist
if($('option[value="' + player + '"]')) {
//Remove player from datalist meaning other inputs cannot add the player
$('option[value="' + player + '"]').remove();
}
//If changing a player, you need to add the old player back into the datalist. This code does not do that for you.
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<datalist id="players">
<option value="1">
<option value="2">
<option value="3">
<option value="4">
<option value="5">
<!-- etc. -->
</datalist>
<input id="CF" onchange="resetPlayerList('CF')" list="players"> <!-- Center Forward -->
<br/><br/>
<input id="LW" onchange="resetPlayerList('LW')" list="players"> <!-- Left Wing -->
This will remove the player from the list using JavaScript/JQuery.
If you want to change a player after adding them in, you will need to do more work, to first re-add the old set player back to the datalist, before removing the new player from it... does that make sense?
UPDATE
<datalist id="players">
<?php
$players = array(
array(
'name' => 'Player 1'
),
array(
'name' => 'Player 2'
)
);
//$players should be your array/object of players from SQL.
for($players as $player) {
echo '<option value="' . $player['name'] . '" />'; //or $player->name if object
}
?>
</datalist>

populating a second select box after selecting a first select box value

I need to select second select box value only after selecting the first select box.I want to insert these values into database.. My view page is below.
<div class="form-group">
<label for="" class="control-label col-xs-2">Country</label>
<div class="col-md-6">
<select class="form-control" id="edu" onChange="EduBackgrd()" name="country">
<option value="">Select</option>
<option value='India'>India</option>
<option value='United States'>United States</option>
<option value='United Kingdom'>United Kingdom</option>
<option value='Canada'>Canada</option>
<option value='Singapore'>Singapore</option>
<option value='Others'>Others</option>
</select>
</div>
</div>
<div class="form-group">
<label for="" class="control-label col-xs-2">Location</label>
<div class="col-md-6">
<select class="form-control" id="edct" name="location">
<option value="">Select</option>
<script>
var eduBak = {};
eduBak['India'] = ['Delhi', 'Mumbai', 'Chennai', 'Kolkata', 'Bangalore', 'Hyderabade', 'Pune'];
eduBak['United States'] = ['Alabama', 'Alaska', 'Arizona'];
eduBak['United Kingdom'] = ['England', 'Northrn Ireland', 'Scotland', 'Wales', 'other'];
eduBak['Canada'] = ['Alberta ', 'Brirish Columbia', 'Manitoba', 'others'];
eduBak['Singapore'] = ['Singapore','others'];
eduBak['Others'] = ['Others']
function EduBackgrd() {
var edu = document.getElementById("edu");
var model = document.getElementById("edct");
var educ = edu.options[edu.selectedIndex].value;
while (model.options.length) {
model.remove(0);
}
var ed = eduBak[educ];
if (ed) {
var i;
for (i = 0; i < ed.length; i++) {
var e = new Option(ed[i], i);
model.options.add(e);
}
}
}
</script>
</select>
</div>
</div>
To insert into database I tried my model as:
public function insert()
{
$data=array(
'Country'=>$this->input->post('country'),
'Location'=>$this->input->post('location'),
);
$this->db->insert('tbl_employer', $data);
}
I'm getting a number while inserting.. I don't know how to set value attribute in the script which I had written for selecting the second select box..How could I fix this problem?
You need to use $.ajax() for this like,
$(function(){
$('#edu').on('change',function(){
$.ajax({
url:'php-page.php', // php page to get options
data:{q:this.value},// passing the value
success:function(data){
$('#edct').html(data);//append options
}
})
});
});
Your php-page.php should look like,
<?php
$eduBak=array();
$eduBak['India'] = ['Delhi', 'Mumbai', 'Chennai', 'Kolkata', 'Bangalore', 'Hyderabade', 'Pune'];
$eduBak['United States'] = ['Alabama', 'Alaska', 'Arizona'];
$eduBak['United Kingdom'] = ['England', 'Northrn Ireland', 'Scotland', 'Wales', 'other'];
$eduBak['Canada'] = ['Alberta ', 'Brirish Columbia', 'Manitoba', 'others'];
$eduBak['Singapore'] = ['Singapore','others'];
$eduBak['Others'] = ['Others'];
$q=isset($_REQUEST['q']) ? $_REQUEST['q'] : '';
$str='';
if($q){
$arr=$eduBak[$q];
if(!empty($arr)){
foreach($arr as $val){
$str.='<option value="'.$val.'">'.$val.'</option>';
}
}
}
echo $str;
exit;
?>
UPDATE: Below is the example of without using PHP
var eduBak = {};
eduBak['India'] = ['Delhi', 'Mumbai', 'Chennai', 'Kolkata', 'Bangalore', 'Hyderabade', 'Pune'];
eduBak['United States'] = ['Alabama', 'Alaska', 'Arizona'];
eduBak['United Kingdom'] = ['England', 'Northrn Ireland', 'Scotland', 'Wales', 'other'];
eduBak['Canada'] = ['Alberta ', 'Brirish Columbia', 'Manitoba', 'others'];
eduBak['Singapore'] = ['Singapore', 'others'];
eduBak['Others'] = ['Others']
$('#edu').on('change',function() {
var edu = document.getElementById("edu");
var model = document.getElementById("edct");
var educ = edu.value;
while (model.options.length) {
model.remove(0);
}
var ed = eduBak[educ];
if (ed) {
var i;
for (i = 0; i < ed.length; i++) {
var e = new Option(ed[i], ed[i]);
model.options.add(e);
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<label for="" class="control-label col-xs-2">Country</label>
<div class="col-md-6">
<select class="form-control" id="edu" name="country">
<option value="">Select</option>
<option value='India'>India</option>
<option value='United States'>United States</option>
<option value='United Kingdom'>United Kingdom</option>
<option value='Canada'>Canada</option>
<option value='Singapore'>Singapore</option>
<option value='Others'>Others</option>
</select>
</div>
</div>
<div class="form-group">
<label for="" class="control-label col-xs-2">Location</label>
<div class="col-md-6">
<select class="form-control" id="edct" name="location">
<option value="">Select</option>
</select>
</div>
</div>

How to disable already selected items in the dropdown list

I have a week schedule. when I am trying to select the already selected item it will get disabled. In my form I am looking to edit the weekdays. If monday is selected then it can't be selected again.
<?
$i=1;
foreach($shop_timing->result() as $name)
{?>
<br>
<input type="hidden" id="shop_time_id" name="time<?echo $i;?>" value="<?echo $name->shop_time_id;?>">
<select class="right" data-placeholder="select your category" id="select_7<?echo $i?>" data-rel="chosen" name="day<?echo $i;?>" >
<option <?php if($name->working_day=="Monday"){?> selected="selected" <?}?> >Monday</option>
<option <?php if($name->working_day=="Tuesday"){?> selected="selected" <?}?>>Tuesday</option>
<option <?php if($name->working_day=="Wednesday"){?> selected="selected" <?}?>>Wednesday</option>
<option <?php if($name->working_day=="Thursday"){?> selected="selected" <?}?>>Thursday</option>
<option <?php if($name->working_day=="Friday"){?> selected="selected" <?}?>>Friday</option>
<option <?php if($name->working_day=="Saturday"){?> selected="selected" <?}?>>Saturday</option>
<option <?php if($name->working_day=="Sunday"){?> selected="selected" <?}?>>Sunday</option>
-->
</select>
<div class="bootstrap-timepicker">
<div class="input-group">
<input type="text" class="timepicker starttime" value="<?echo $name->working_start_time?>" name="timefrom<?echo $i;?>" id="timefrom" placeholder="time"/>
</div>
</div>
<div class="bootstrap-timepicker">
<div class="input-group">
<input type="text" class="timepicker end" value="<?echo $name->working_end_time?>" name="endtime<?echo $i;?>" id="endtime" placeholder="time"/>
</div>
</div>
<? $i++; } ?>
<input type="hidden" id="count" name="count" value="<?echo $count;?>">
<?
for($j=1;$j<=(7-$count);$j++)
{?>
<select class="right" data-placeholder="select your category" id="select_7<?echo $i?>" data-rel="chosen" name="day<?echo $i;?>">
<option disabled="disabled" selected="selected">Day</option>
<option>Monday</option>
<option>Tuesday</option>
<option>Wednesday</option>
<option>Thursday</option>
<option>Friday</option>
<option>Saturday</option>
<option>Sunday</option>
</select>
<div class="bootstrap-timepicker">
<div class="input-group">
<input type="text" class="timepicker starttime" name="timefrom<?echo $i;?>" id="starttime3" placeholder="time"/>
</div>
</div>
<div class="bootstrap-timepicker">
<div class="input-group">
<input type="text" class="timepicker end" name="endtime<?echo $i;?>" id="endtime3" placeholder="time"/>
</div>
</div>
<? $i++; } ?>
You are trying to select multiple items in a dropdown but you have not told the dropdown that multiple selection is allowed. I think this is confusing things
On your <select..> add the multiple attribute like so
<select multiple
class="right"
data-placeholder="select your category"
id="select_7<?echo $i?>"
data-rel="chosen"
name="day<?echo $i;?>" >
Well first of all for the purpose of DRY, don't repeat yourself. The easy way to build your select is with an array and a loop . Typically it would take this form
<?php
function printOptions( $selected_value )
$days = array(
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
);
foreach( $days as $day ){
if($selected_value == $day ){
$selected = ' selected="selected"';
}else{
$selected = '';
}
echo '<option value="'.$day.'"'.$selected.' >'.$day.'</option>'
}
}
?>
Then you can go
<select class="right day_selection" data-placeholder="select your category" id="select_7<?echo $i?>" data-rel="chosen" name="day<?echo $i;?>" >
<?php printOptions( $name->working_day ) ?>
</select>
Isn't that much nicer looking
That settled this is largely a javascript problem so with jquery you would want to do something like this:
var selectors = $('day_selection'); //cache select elments
$('.day_selection').change( function(){
var current = $(this).prop('id');
var current_value = $(this).val();
$.each( selectors, function(i,v){
if($(this).prop('id') != current ){
$(this).find('option[value="'+current_value+'"]').prop('disabled', true );
}
});
});
now you will have to find a way to un-disable them but that should point you in the right direction. Likely you could make an object to store the values in and then when they change look up that value and disable,false them.
Like this
var selected = {
"select_71" : "Monday" //Id : value
}
And below this
$(this).find('option[value="'+current_value+'"]').prop('disabled', true );
Add
if( selected.current ){
$(this).find('option[value="'+selected.current+'"]').prop('disabled', false );
}
And outside ( after ) the each loop add:
selected.current = current_value;
On a side note an Idea that occurs to me is why have the selects at all, why not just have blocks with a checkbox and the time range, checking the checkbox selects the day. Then there is no need to worry about it and the order is fixed.
Thursday 8/20/2015 [ ] select
[_ start time ] to [ end time _]
Friday 8/21/2015 [ ] select
[_ start time ] to [ end time _]
etc...

Categories