Dynamic drop down box - php

I am trying to create 3 drop down boxes dependent on each other. Each drop down box is getting its data from its own tables. There are 3 tables as shown:
This is the form:
<label for="tourtype">
Tour Type
</label>
<select id="tourtype" name="tourtype" required>
<option value="" selected="selected">
--Select--
</option>
<?php
$sql=mysql_query("Select tour_type_id,tour_name from tour_type");
while($row=mysql_fetch_array($sql))
{
$tour_type_id=$row['tour_type_id'];
$name=$row['tour_name'];
echo "<option value='$tour_type_id'>$name</option>";
}
?>
</select>
<label>
Country
</label>
<select id="country" name="country" class="country" required>
<option value="" selected="selected">
-- Select --
</option>
</select>
<
<label>
Destination
</label>
<select id="destination" name="destination" class="destination" required>
<option value="" selected="selected">
-- Select --
</option>
</select>
This is the javascript:
<script type="text/javascript">
$('#tour_type').change(function () {
var id = $(this).val();
$.ajax({
type: "POST",
url: "ajax.php",
data: "&id=" + id + "&get_countries=1",
success: function (html) {
$("#country").html(html);
}
});
});
$('#country').change(function () {
var id = $(this).val();
$.ajax({
type: "POST",
url: "ajax.php",
data: "&id=" + id + "&get_destination=1",
success: function (html) {
$("#destination").html(html);
}
});
});
</script>
And this is the ajax.php
<?php
include ('../config.php');
< ? php
if ($_REQUEST['get_countries']) {
$sql = mysql_query("SELECT * FROM `countries` where `tour_type_id`=" . $_REQUEST['id']);
$countries = "";
while ($row = mysql_fetch_array($sql)) {
$cid = $row['countries_id'];
$name = $row['countries_name'];
$countries.= "<option value='" . $cid . "'>" . $name . "</option>";
}
echo $countries;
}
elseif ($_REQUEST['get_destination']) {
$destination = "";
$sql = mysql_query("SELECT * FROM `destination` where `country_id` =" . $_REQUEST['id'])
while ($row = mysql_fetch_array($sql)) {
$destination_id = $row['destination_id'];
$name = $row['destination_name'];
$destination.= "<option value='" . $destination_id . "'>" . $name . "</option>";
}
echo $destination;
}
?>
The problem is the 2nd and 3rd drop down boxes are not populating anything. Can anyone help me? For example if i select culture on the 1st drop down, the 2nd drop down should show Holland and Belgium. Then if i select Holland, the 3rd drop down should show Amsterdam.

Your identifier in your Javascript is #tour_type when your id is #tourtype.
If the syntax is correct and your SQL results are correct too, it should work.
EDIT: some of your JS isn't right.
data: "&id=" + id + "&get_countries=1",
should be
data: {id: id, get_countries: 1},
You should also put a debug on your ajax call by adding
error: function () { alert("ajax failed"); }
after your success callback
full sources now:
$('#tourtype').change(function() {
var id=$(this).val();
$.ajax
({
type: "POST",
url:"jurassicbase5/admin/ajax.php",
data: {id: id, get_countries: 1},
success: function(html){
$("#country").empty();
$("#country").append(html);
},
error: function () { alert("ajax failed"); }
});
});
$('#country').change(function() {
var id=$(this).val();
$.ajax
({
type: "POST",
url: "jurassicbase5/admin/ajax.php",
data: {id: id, get_destination: 1},
success: function(html)
{
$("#destination").empty();
$("#destination").append(html);
},
error: function () { alert("ajax failed"); }
});
});

Related

multiple dependent dropdown in add page and edit page in codeigniter

I need to add multiple dropdowns to my project and it also needs a dependent multiple dropdowns
The existing dropdown category should be changed to multiple dropdowns
Automatically they select values in its dependent subcategory multiple dropdowns it its possible ?
Current Single Choice Dependent Dropdown How To Make Multiple Choose Dependent Dropdown?
please help me
view page
<label for="category_id"><?php echo get_phrase('category'); ?> <span
class="text-danger">*</span></label>
<select class="form-control selectpicker" name="category_id" id="category_id" required multiple>
<option value="" hidden><?php echo get_phrase('select category'); ?></option>
<?php
$roles = $this->db->get('category_table')->result_array();
foreach($roles as $row):
?>
<option value="<?php echo $row['category_id'];?>">
<?php echo $row['category_name'];?>
</option>
<?php
endforeach;
?>
</select>
<label for="idsubcategory"><?php echo get_phrase('Sub_category'); ?>
<span class="text-danger"></span> </label>
<select class="form-control" name="idsubcategory" id="idsubcategory"
placeholder="subcategory" multiple>
<option value="" hidden><?php echo get_phrase('select subcategory'); ?></option>
<?php
$roles = $this->db->get('sub_category')->result_array();
foreach($roles as $row):
?>
<option value="<?php echo $row['idsubcategory'];?>">
<?php echo $row['subcategory_name'];?>
</option>
<?php
endforeach;
?>
</select>
ajax
$(document).ready(function()
{
$('#category_id').change(function() {
var category_id = $(this).val();
$.ajax({
url: "<?php echo site_url('admin/get_sub_category');?>",
method: "POST",
data: {
category_id: category_id
},
async: true,
dataType: 'json',
success: function(data) {
var html = '';
var i;
for (i = 0; i < data.length; i++) {
html += '<option value=' + data[i].idsubcategory + '>' + data[i]
.subcategory_name + '</option>';
}
$('#idsubcategory').html(html);
}
});
return false;
});
});
controller
function get_sub_category(){
$category_id = $this->input->post('category_id',TRUE);
$data = $this->Crud_model->get_sub_category($category_id)->result();
echo json_encode($data);
}
model
function get_sub_category($category_id){
$query = $this->db->get_where('sub_category', array('category_id' => $category_id));
return $query;
}
<select id="select1">
<option value='opt1'>Option 1</option>
<option value='opt2'>Option 2</option>
<option value='opt3'>Option 3</option>
</select>
<select id="select2">
<select>
<select id="select3">
<select>
<script>
$('#select1').change(function() {
var category_id = $(this).val();
$.ajax({
url: "<?php echo site_url('controller/function_for_second_dropdown');?>",
method: "POST",
data: {
category_id: category_id
},
async: true,
dataType: 'json',
success: function(data) {
var html = '';
var i;
for (i = 0; i < data.length; i++) {
html += '<option value=' + data[i].idsubcategory + '>' + data[i].your_option+ '</option>';
}
$('#select2').append(html);
}
});
$('#select2').change(function() {
var select2= $(this).val();
$.ajax({
url: "<?php echo site_url('controller/function_for_third_dropdown');?>",
method: "POST",
data: {
select2: select2
},
async: true,
dataType: 'json',
success: function(data) {
var html = '';
var i;
for (i = 0; i < data.length; i++) {
html += '<option value=' + data[i].your_option + '>' + data[i].your_option+ '</option>';
}
$('#select2').append(html);
}
});
</script>

How to get data based on 1st table id contain in 2nd table using ajax codeigniter on dropdown lists

I have three tables like tbl_customers, tbl_items, tbl_customersOrders. I inserted the tbl_customers ID and tbl_items ID into the tbl_customersOrders which as a foreign key. Now think is based on first drop-down lists (here the tbl_customersOrders ID) I can fetch another two fields on other drop-down lists but .... I need the name of the customers which is id matched...also items name which is id matched Like:
what I tried:
Controller Code:
$data['gets_order'] = $this->box_property_model->getCustDataOrder();
Model Code:
public function getCustDataOrder()
{
$this->db->select('*');
$this->db->order_by('customer_orders_id', 'DESC');
$query = $this->db->get('tbl_customer_orders');
return $query->result();
}
View code:
<select name="nestedOrder" class="form-control" >
<option value="">--- Select Order ---</option>
<?php if(!empty($gets_order)) {
foreach($gets_order as $nst){
?>
<option value="<?php echo $nst->customer_orders_id; ?>"><?php echo $nst->new_id_generated; ?></option>';
<?php } } ?>
</select>
<label for="custNested">Customer Name-</label>
<select name="custNested" id="custNested" class="form-control" >
<option value="">Select ITEMS ID</option>
</select>
<label for="ItemsName">Items Name</label>
<select name="items_ids" id="getsitemsName" class="form-control" >
<option value="">Select ITEMS</option>
</select>
<label for="testing">Final ITEM Name</label>
<select name="org_itemsNameValue" id="org_itemsNameValue" class="form-control" >
<option value="">Select ITEMS Name</option>
</select>
JS CODE:
<script type="text/javascript">
$(document).ready(function() {
$('select[name="nestedOrder"]').on('change', function() {
var customID = $(this).val();
alert(customID);
$.ajax({
url: '<?php echo base_url();?>/Box_property/DropdownsAjaxNested/'+customID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="custNested"]').empty();
$('select[name="custNested"]').append('<option value="0">--Select CUSTTT--</option>');
$.each(data, function(key, value) {
$('select[name="custNested"]').append('<option value="'+value.customer_id + '">'+ value.customer_name +'</option>');
});
}
});
$.ajax({
url: '<?php echo base_url();?>/Box_property/DropdownsAjaxNestedItems/'+customID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="items_ids"]').empty();
$('select[name="items_ids"]').append('<option value="0">--Select --</option>');
$.each(data, function(key, value) {
$('select[name="items_ids"]').append('<option value="'+value.items_id +'">'+ value.items_id +'</option>');
});
}
});
});
$('select[name="items_ids"]').on('change', function() {
var items_ids = $(this).val();
alert(items_ids);
$.ajax({
url: '<?php echo base_url();?>/Box_property/itemsOrdersIDfetch/'+items_ids,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="org_itemsNameValue"]').empty();
$('select[name="org_itemsNameValue"]').append('<option value="0">--Select ITEMS Name--</option>');
$.each(data, function(key, value) {
$('select[name="org_itemsNameValue"]').append('<option value="'+value.items_id + '">'+ value.items_name +'</option>');
});
}
});
});
});
</script>
In Controller:
public function DropdownsAjaxNested($id) {
$result = $this->db->where("customer_id",$id)->get("tbl_customers")->result();
echo json_encode($result);
}
public function DropdownsAjaxNestedItems($id) {
$result = $this->db->where("customer_orders_id",$id)->get("tbl_items_order")->result();
echo json_encode($result);
}
public function itemsOrdersIDfetch($id){
$result = $this->db->where("items_id",$id)->get("tbl_items_master")->result();
echo json_encode($result);
}

Dependent dropdown menu using ajax not refreshing but console is showing data

I am using a MVC structure and here is the JQuery:
<script type="text/javascript">
$('select[name="provider_id"]').change(function() {
var provider_id = $(this).val();
if (provider_id) {
$.ajax({
url: url + '/ProductsAjax/GetOutletByProvider',
type: 'POST',
dataType: 'json',
data: {
provider_id: provider_id
},
})
.done(function(data) {
$.each(data, function(index, item) {
var outlets = "<option value='" + item.id + "'>" + item.outlet_name + "</option>";
$('select[name="outlet"]').append(outlets);
console.log(item.id + ' ' + item.outlet_name);
})
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log(textStatus + ': ' + errorThrown);
console.warn(jqXHR.responseText);
});
} else {
$('select[name="outlet"]').empty();
}
});
</script>
It is outputting the correct data in console but the dropdown menu itself does not show the data at all. Nothing happens.
Here are the select menus:
<div class="form-group m-form__group">
<label for="example_input_full_name">
Select Service Provider
</label>
<select class="form-control m-bootstrap-select m_selectpicker" name="provider_id">
<option value="">Select Service Provider</option>
<?php foreach($data['sproviders'] as $service_provider): ?>
<option value="<?php echo $service_provider->service_provider_id; ?>"><?php echo $service_provider->sp_name; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group m-form__group">
<label for="example_input_full_name">
Select Outlet
</label>
<select class="form-control m-bootstrap-select m_selectpicker" name="outlet" id="outlet">
</select>
</div>
Since the correct data is displaying in console, I can't figure out why it isn't pulling through into the second select menu.
Console data:
108 Branch One
109 Branch Two
110 Branch Three
<script type="text/javascript">
$('select[name="provider_id"]').change(function() {
var provider_id = $(this).val();
if (provider_id) {
$.ajax({
url: url + '/ProductsAjax/GetOutletByProvider',
type: 'POST',
dataType: 'json',
data: {
provider_id: provider_id
},
})
.done(function(data) {
var outlets="";
$.each(data, function(index, item) {
outlets += "<option value='" + item.id + "'>" + item.outlet_name + "</option>"; // concat all options
console.log(item.id + ' ' + item.outlet_name);
})
$('select[name="outlet"]').html(outlets); // add all options
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log(textStatus + ': ' + errorThrown);
console.warn(jqXHR.responseText);
});
} else {
$('select[name="outlet"]').empty();
}
});
</script>

how to get select value derived from php with ajax

I have two files, one index.php and the other get_content.php, unfortunately I can not show anything on get_content.php, I am confused about where my fault lies either in index.php or get_content.php?
Full Code
Click here
index.php
AJAX :
$("#id_content").click(function() {
var id_content = $("#id_content").val();
$.ajax({
type:"POST",
url:"get_content.php",
data: "id_content="+id_content,
cache:false,
success:function(msg){
$("#result_of_ajax").html(msg);
}
})
})
PHP :
<select id='id_content' name='id_content'>
<option value='ID Content'>Content</option>
<?php
$sql = "select id_content from content where status = 'setuju'";
$hasil = mysqli_query($konek, $sql);
while($data = mysqli_fetch_assoc($hasil)) {
echo "<option value='$data[id_content]'>$data[content]</option>";
}
?>
</select>
and here is get_content.php
get_content.php
echo $_POST['id_content'];
why in get_content.php does not show anything ???
Sorry it is not an answer yet. But I believe you should change your jQuery+AJAX code a bit like:
$("#id_content").on('change', function() {
var id_content = this.value;
$.ajax({
type:"POST",
url:"get_content.php",
data: { id_content: id_content },
cache:false,
success:function(msg){
$("#result_of_ajax").html(msg);
}
})
})
You have done mistake here `echo "<option value='$data[id_content]'>$data[content]</option>";`
Change to
<option value=<?echo $data['id_content'];?>><?echo $data['content'];?></option>
And one more change was there.. check below..
<select id='id_content' name='id_content'>
<option value='ID Content'>Content</option>
<?php
$sql = "select id_content from content where status = 'setuju'";
$hasil = mysqli_query($konek, $sql);
while($data = mysqli_fetch_assoc($hasil))
{?>
<option value=<?echo $data['id_content'];?>><?echo $data['content'];?></option>
<?}
?>
</select>
<br>
<div id="result_of_ajax">
</div>
$("#id_content").on('change', function() {
var id_content = $('#id_content').val();
$.ajax({
type:"GET",
url:"get_content.php?id_content="+id_content,
cache:false,
success:function(msg){
$("#result_of_ajax").html(msg);
}
})
})
get_content.php
<?echo $_GET['id_content'];?>

Dropdown value based Autocomplete

I am trying to do autocomplete function based on dropdown selection value.
My index.php page:
<div>Select City</div>
<select id="city">
<option value="select">Select</option>
<option value="Chennai">Chennai</option>
<option value="Madurai">Madurai</option>
<option value="Salem">Salem</option>
<option value="Trichy">Trichy</option>
</select><br />
<input type="text" id="search" />
<script>
$(document).ready(function(){
$('#search').autocomplete({
minLength: 1,
source: function(query, process) {
var res = $('#city').val();
$.ajax({
url: 'autocomplete.php',
type: 'GET',
data: "src="+res + "&value=" + $('#search').val(),
dataType: 'JSON',
async: true,
success: function(data) {
process(data);
}
});
}
});
});
</script>
My autocomplete.php page:
<?php
if (isset($_GET['src'])) {
$city = $_GET['src'];
echo $city;
}
?>
<?php
$loc = $_GET['value'];
echo $loc;
$conn=mysql_connect('localhost', 'root', '');
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
if(!mysql_select_db("details"))
{
die('Could not connect database: ' . mysql_error());
}
$sql="SELECT localty FROM localty where localty like '%".$loc."%' and city='$city'";
echo $sql;
$result = mysql_query($sql);
if($result)
{
while($row=mysql_fetch_array($result))
{
echo $row['localty']."\n";
}
}
?>
Based on the city selection dropdown value,in search textbox localty should be displayed on entering first letter.But I'm not getting values in autocomplete.php page.
Please give any suggestions.
try this ...
<script>
$(document).ready(function(){
$('#city').change(function(){
$('#search').autocomplete({
minLength: 1,
source: function(query, process) {
var res = $('#city').val();
$.ajax({
url: 'autocomplete.php',
type: 'GET',
data: "src="+res + "&value=" + $('#search').val(),
dataType: 'JSON',
async: true,
success: function(data) {
process(data);
}
});
});
}
});
});
</script>

Categories