how to Load Dependent Dropdown on Multi-Select in laravel - javascript

I want to load dependent dropdwon on multiselect in laravel.how can i load ?now i have sigle select to load dropdown .below is my code i want to load dependent dropdwon on multiselect in laravel.how can i load ?now i have sigle select to load dropdown .below is my code
<select class="form-control" id="region_country_id" name="region_country_id" >
<?php foreach ($countries as $key => $value) { ?>
<option value="<?php echo $value->id; ?>"<?php if($product->region_country_id == $value->id){ echo "selected";} ?>>
<?php echo $value->name;?>
</option>
<?php } ?>
</select>
<select class="form-control" id="region_id" name="region_id" style="margin-top: 10px;" >
<?php if(Session::get('branch_access') != 1)
{?>
<option value="">All region</option>
<?php } ?>
<?php foreach ($regions as $key => $value) { ?>
<option value="<?php echo $value->id; ?>" <?php if($value->id == $product->region_id) { echo "selected";} ?>><?php echo $value->region; ?>
</option>
<?php } ?>
</select>
below is script for above
$('body').on('change', '#region_country_id', function() {
loadOptionRegion($(this).val());
});
function loadOptionRegion(countryID) {
$.ajax({
method: "POST",
url: "/region/country",
dataType: 'json',
data: {
'country_id': countryID
},
beforeSend: function() {},
success: function(data) {
console.log(data.data.region);
var regionList = data.data.region;
var str = '<option value="0">All Region</option>';
$.each(regionList, function(index, value) {
str = str + "<option value='" + value.id + "'>" + value.region + "</option>";
console.log(str);
});
$("#region_id").html(str);
}
})
}

Very first add the multiple in the select box
<select class="form-control" id="region_country_id" name="region_country_id" multiple>
Now in your controller change the query from where to whereIn country ID
From:
->where('country_id',$countryId);
To:
->whereIn('country_id',$arrOfCountryID);

Related

changing select box on another select box value not working in codeigniter

i have a codeigniter website, where there is a form for user to select category and subcategory, when a user selects category the subcategory dropdown should show accordingly, i did the following code:
public function subcategories() {
$category = $this->input->post('category');
$query = $this->db->query('SELECT name FROM subcategory WHERE parentcategory='.$category);
$data['subcategories'] = $query->result_array();
$this->load->view('homecontroller/subcategories', $data);
echo $category;
}
in subcategories.php
<?php foreach ($subcategories as $c): ?>
<option value="<?php echo $c['name'] ?>"><?php echo $c['name'] ?></option>
<?php endforeach; ?>
and in my view:
$(document).ready(function() {
$('#sl_countries').change(function() {
$('#sl_cities').load("<?php echo site_url('index.php/homecontroller/subcategories') ?>", {
category: $(this).val()
});
});
});
<div class="form-group col-md-6">
<label for="inputEmail4">Product Category</label>
<select id="sl_countries" class="form-control" name="cname" aria-label="Default select example">
<?php
foreach($listcategory as $val){
echo '<option value="'.$val->name.'">'.$val->name.'</option>';
}?>
</select>
</div>
<div class="form-group col-md-6">
<label for="inputEmail4">Sub Category</label>
<select class="form-control" name="sname" id="sl_cities"></select>
</div>
however this is not working, i get an error like below when i checked the console:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
SELECT name FROM subcategory WHERE parentcategory=
can anyone please tell me what is wrong here, thanks in advance
use this way:-
jQuery Ajax code :-
<script>
$(document).ready(function(){
$('#country').change(function(){
var country_id = $('#country').val();
if(country_id != '')
{
$.ajax({
url:"<?php echo base_url(); ?>dynamic_dependent/fetch_state",
method:"POST",
data:{country_id:country_id},
success:function(data)
{
$('#state').html(data);
$('#city').html('<option value="">Select City</option>');
}
});
}
else
{
$('#state').html('<option value="">Select State</option>');
$('#city').html('<option value="">Select City</option>');
}
});
});
</script>
Controller Code :-
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dynamic_dependent extends CI_Controller {
function fetch_state()
{
if($this->input->post('country_id'))
{
echo $this->dynamic_dependent_model->fetch_state($this->input->post('country_id'));
}
}
}
?>
Model Code :-
<?php
class Dynamic_dependent_model extends CI_Model
{
function fetch_state($country_id)
{
$this->db->where('country_id', $country_id);
$this->db->order_by('state_name', 'ASC');
$query = $this->db->get('state');
$output = '<option value="">Select State</option>';
foreach($query->result() as $row)
{
$output .= '<option value="'.$row->state_id.'">'.$row->state_name.'</option>';
}
return $output;
}
}
?>
View Code :-
<div class="form-group">
<select name="country" id="country" class="form-control input-lg">
<option value="">Select Country</option>
<?php
foreach($country as $row)
{
echo '<option value="'.$row->country_id.'">'.$row->country_name.'</option>';
}
?>
</select>
</div>
<br />
<div class="form-group">
<select name="state" id="state" class="form-control input-lg">
<option value="">Select State</option>
</select>
</div>

Splice method in codeigniter drop down

I have some locations that are stored in my database separated by a comma and I have a dropdown that gets that information. The user selects a location to populate another drop down based on this chosen location.
Here is my php code:
<label for="select-service">
<strong>Enter a Location:</strong>
</label>
<select class="form-control" id="select-location" class="col-xs-12 col-sm-4 form-control" required>
<option value="">Select Location</option>
<?php
foreach($appointment_locations as $location) {
?>
<option value="<?php echo $location->notes ?>"><?php echo $location->notes ?></option>
<?php
}
?>
</select>
Here is my javascript code:
$(document).ready(function() {
FrontendBook.initialize(true, GlobalVariables.manageMode);
GeneralFunctions.enableLanguageSelection($('#select-language'));
$('#select-provider').html('');
$('#select-location').change(function() {
$('#select-provider').html('');
var selected_location = $(this).val();
$.ajax({
url: '<?php echo site_url('appointments/getProviderByLocation'); ?>',
type: 'POST',
data: {
csrfToken: GlobalVariables.csrfToken,
'selected_location': selected_location,
},
dataType: 'json',
success: function(data) {
var options = '';
$.each(data, function(key,val) {
console.log(val.id);
options += '<option value="'+val.id+'">'+val.first_name+" " +val.last_name +'</option>'
});
$('#select-provider').html(options);
}
});
});
and here is a screenshot of the location how it is currently:
So what i want to achieve is to have Randburg as one option, Greenside as another option and Rosebank as another option.
You have different type string in your array so for this particular problem in your loop you should explode your string like this and another loop to print separated string
<select class="form-control" id="select-location" class="col-xs-12 col-sm-4 form-control" required>
<option value="">Select Location</option>
<?php
foreach($appointment_locations as $location) {
$LocationsArray = explode(",", $location->notes);
foreach($LocationsArray as $singleLocation):
?>
<option value="<?=$singleLocation ?>"><?=$singleLocation ?></option>
<? endforeach;
};?>

Cannot get corresponding value from one select box

I am working on a registration page where a user when selects its designation, its corresponding reporting head gets selected in "reporting head" field dynamically. I have used Ajax for this but somehow the reporting head field remains empty. Can I get some insights over this?? It will be highly appreciated.
Here goes my registration fields,
<select name="designation" class="form-control" required="" id="desig" >
<option selected="selected" >Select your option</option>
<?php
$sql = mysql_query("SELECT id, designation FROM tbl_designation where id in (1,2,3,4)");
while ($row = mysql_fetch_assoc($sql)){
echo "<option value=" . $row['id'] . ">" . $row['designation'] . "</option>";
}
?></select>
<select name="reporting_head" class="form-control" required="" id="rephead">
<option selected="selected">Select your option</option>
</select>
Now JavaScript+AJAX that I am applying,
$(document).ready(function()
{
$("#desig").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "ajax_rep.php",
data: dataString,
cache: false,
success: function(html)
{
$("#rephead").html(html);
}
});
});
});
And finally my ajax_rep.php
<?php
require("includes/config.php");
require("classes/Database.class.php");
if($_POST['id'])
{
$id=$_POST['id'];
$sql=mysql_query("SELECT reporting_head_id, reporting_head FROM `tbl_designation` WHERE id='$id'");
while($row=mysql_fetch_array($sql))
{
$id=$row['reporting_head_id'];
$data=$row['reporting_head'];
echo '<option value="'.$id.'">'.$data.'</option>';
}
}
?>
This is the HTML I am getting on ajax success,
<div class="row row-offcanvas row-offcanvas-right">
<!--/.col-xs-12.col-sm-9-->
<div class="col-sm-3 col-md-3 sidebar" id="sidebar">
<div id="left_panel" class="clearfix left">
<option value="6">Sales Head</option>
try this in you loop while
$list = '<option selected="selected">Select your option</option>';
while($row=mysql_fetch_array($sql))
{
$id=$row['reporting_head_id'];
$data=$row['reporting_head'];
$list .= '<option value="'.$id.'">'.$data.'</option>';
}
}
echo $list;
And in your ajax success function do this
success: function(html)
{
$("#rephead").html("");
$("#rephead").append(html);
}

javascript and ajax to update the second drop dowm and the function undefined error

This is my php file
<select name="scity" id="city_id" class="myinput" required onChange="getMalls()">
<option value="<?php echo $_POST['scity'] ;?>"><?php echo $_POST['scity'] ;? ></option>
<option value="">Please Select</option>
<?php
include 'include/allcity.php';
?>
</select>
<select name="mallname" required class="myinput" id="malls-list">
<option value="">Please Select</option>
</select>
<script>
function getMalls() {
document.getElementById("city_id").value;
alert(hi);
$.ajax({
type: "POST",
url: "include/allmall.php";
data:'city_id='+x,
success: function(data){
$("#malls-list").html(data);
}});
}
</script>
<?php
alert(hi);
include 'mydb.php';
if(!$con) {
echo 'Could not connect to the database.';
} else {
if(!empty($_POST['scity'])) {
$queryString = $con->real_escape_string($_POST['scity']);
if(strlen($queryString) > 0) {
$querys = $con->query("SELECT mallname FROM mallname WHERE cityname = '%$queryString%' order by mallname");
if($querys) {
while ($results = $querys -> fetch_object()) {
<option value="<?php echo ($results -> $results["id"]; ?>"><?php echo ($results -> $results["mallname"]; ?></option>
}
} else {
echo 'OOPS we had a problem :(';
}
} else {
// do nothing
}
} else {
echo 'There should be no direct access to this script!';
}
}
?>
There is another php file after the script tag where it is referring to.
First of all I am getting an error that function is not defined. Actually this is for activating second dropdown on the value of first dropdown. Please help me. Thanks in advance.

onchange function when selecting drop down menu will populate a textbox

this is my table in the database
tbl_job_title
ID | Job_title | description |
1 | Accountant | Handle Accounts |
2 | Dev. Support | Support Centre |
when selecting the job title from the table, i'm using a drop down menu and here is my code.
<select id="job_title" >
<option value=""> </option>
<?php foreach ($job_title as $row): ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?>
</option>
<?php endforeach?>
</select>
and I have a textbox that will populate if i select a job_title
<input type="text" id="catch_value">
How do I populate the textbox when I select example is "Accountant", the textbox value must be "Handle Accounts".
Here is the short & sweet code I have made just for you, feel free to use it!
Not required Ajax...
CODE -
<select id="job_title" >
<option value="" class="job" role=""> </option>
<?php foreach ($job_title as $row): ?>
<option value="<?php echo $row['id']; ?>" class="job<?php echo $row['id']; ?>" role="<?php echo $row['description']; ?>"><?php echo $row['title']; ?>
</option>
<?php endforeach?>
</select>
Jquery -
$('#job_title').on('change',function(){
var m = this.value;
var n = $('.job'+m).attr('role');
$('#catch_value').val(n);
return false;
});
eg - fiddle
<select id="job_title" onchange="javascript:callajaxvalue(this.value);">
<option value=""> </option>
<?php foreach ($job_title as $row): ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?>
</option>
<?php endforeach?>
</select>
<script>
function callajaxvalue(id)
{
$.ajax({
type: "POST",
url: "some.php",
data: { id: id}
success: function(data, textStatus ){
if(data)
{ $("#catch_value").val(data);}
},
})
}
</script>
some.php
$_POST['id'];
$query = "get data from table";
execute query.
echo $row['description'];
For codeigniter
function callajaxvalue(id)
{
$.ajax({
type: "POST",
url: "my_controller/update/"+id,
data: { id: id}
success: function(data, textStatus ){
if(data)
{ $("#catch_value").val(data);}
},
})
}
my_Controller.php
public function update()
{
$id = $this->input->post('id');
$data['discription'] = call model function for get data form database
echo json_encode($data);
}
$('#job_title').change(function(){
$('#catch_value').val('Handle Accounts');
});
see fiddle:fiddle
or you can add if statments
$('#job_title').change(function(){
var jt = $('#job_title').val();
if(jt == "Something")
{
$('#catch_value').val('Handle Accounts');
}
if (jt == "SomethingElse")
{
$('#catch_value').val('somethingelse');
}
});
This is the right answer :
<select id="job_title" onchange='populate()'>
<option value=""> </option>
<?php foreach ($job_title as $row): ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?>
</option>
<?php endforeach?>
</select>
<script>
function populate(){
var jobttl= document.getElementById('job_title').value();
if(jobttl){
document.getElementById('catch_value').value;
}
}
</script>

Categories