Cannot get corresponding value from one select box - javascript

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);
}

Related

Drop list menu selectpicker printed by php and doesn't interact with jquary

I created a drop-down menu type 'select picker'. This list is printed from a controller-function that is called by a jQuery.
The menu has been called and the result appeared well. the problem the drop down menu appeared as no style and doesn't interact if the another option selected.
select picker printed into the below div with ID [itemresult]
HTML
<form id="myWHForm" action="" method="post" class="form-horizontal" >
<input type="hidden" name="txtItemId" value="0">
<div id="itemresult"></div>
<div class="form-group">
<label for="warehouse" class="label-control col-md-3">Warehouse</label>
<div class="col-md-8">
<select class="selectpicker" required data-show-subtext="true" data-live-search="true" name="Warehouse" id="Warehouse">
<?php foreach($wh as $w):?>
<option value= "<?php echo $w->w_id;?>" ><?php echo $w->w_name;?></option>
<?php endforeach;?>
</select>
</div>
</div>
</form>
jQuery/Ajax [fetch the drop menu from the controller and take another action if the an option of the select picker picked]
$(function() {
load_item_data();
function load_item_data() {
$.ajax({
url: "<?php echo base_url(); ?>ItemSettings/load_item_data",
method: "POST",
success:function (data) {
$('#itemresult').html(data);
}
})
}
$('#item_wh').change(function(){
var wh_result = $(this).val();
//$(#item_wh).val();
if(wh_result != '')
{
load_wh_data(wh_result);
}
});
});
Controller
function load_item_data(){
$output = '';
$data = $this->m->load_item_data();
$output .='<div class="form-group">
<label for="item" class="label-control col-md-3">Item</label>
<div class="col-md-8">
<select class="selectpicker" required data-show-subtext="true" data-live-search="true" name="item_wh" id="item_wh">
<option disabled selected>Select Item</option>
';
if($data->num_rows() > 0)
{
foreach($data->result() as $row)
{
$output .='
<option value ="'.$row->s_id.'">'.$row->s_name.'</option>
';
}
}
$output.='
</select>
</div>
</div>
';
echo $output;
}
Since the dropdown is generated dynamically chaNGE YOUR SCRIPT FUNCTION AS BELOW
$(document).on("change","#item_wh", function()
{
var wh_result = $(this).val();
//$(#item_wh).val();
if(wh_result != '')
{
load_wh_data(wh_result);
}
});

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;
};?>

How to check ajax overlapping and through them out?

I had created a simple on change event in Ajax and trying to get data on the change of select tag [HTML] in another select tag.
All's work fine I got the result on test.php
test.php
<div class="row">
<div class="input-field col s12">
<select name="category" id="category">
<option value="" disabled selected>Choose Category</option>
<?php
include "db-connect/db-connect.php";
$qry = mysqli_query($conn, "SELECT * FROM `category`");
while ($row=mysqli_fetch_assoc($qry)) {
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['category']; ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<select name="subcategory" id="subcategory">
<option value="">Choose Sub Category</option>
</select>
</div>
</div>
and the ajax code for this html :-
<script type="text/javascript">
$(document).ready(function(){
$('#category').on('change',function(){
var categoryID = $(this).val();
if(categoryID){
$.ajax({
type:'POST',
url:'ajax/category.php',
data:'category_id='+categoryID,
success:function(html){
$('#subcategory').html(html);
//$('#city').html('<option value="">Select state first</option>');
}
});
}else{
$('#subcategory').html('<option value="">Choose category first</option>');
//$('#city').html('<option value="">Select state first</option>');
}
});
});
</script>
When I run it I got the result but after that, I want to implement it into my project template I got nothing.
But, I get data in the inspect element [F12] -> Network
Ajax data file ajax/category.php:-
<?php
//Include the database configuration file xD
include "../db-connect/db-connect.php";
if(!empty($_POST["category_id"])){
//Fetch all data
$query = mysqli_query($conn,"SELECT * FROM `sub_category` WHERE `category_id` = ".$_POST['category_id']."");
//option list
if(mysqli_num_rows($query) > 0){
echo '<option value="">Select state</option>';
while($row = mysqli_fetch_assoc($query)){
echo '<option value="'.$row['sub_category'].'">'.$row['sub_category'].'</option>';
}
}else{
echo '<option value="">State not available</option>';
}
}
?>

how to Load Dependent Dropdown on Multi-Select in laravel

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);

Categories