Here is my code
<div class="form-group">
<label for="salary" class="control-label">Department:*
</label>
<select class="form-control" id="selDepartment"
name="selDepartment" autofocus="autofocus">
<option value="-1" onmouseout="" ="">Select
Department</option>
<?php
$query="SELECT * FROM department_master";
$results=mysqli_query($con,$query);
foreach ($results as $dm_department) {
?>
<option value="<?php echo $dm_department["dm_department"];?>";?>
<?php echo $a=$dm_department["dm_department"];?></option>
<?php
}
?>
</select>
</div>
<div class="form-group">
<label for="salary" class="control-label">Designation:*
</label>
<select class="form-control" id="selDesignation"
name="selDesignation" autofocus="autofocus">
<option value="-1" onmouseout="" ="">Select
Designation</option>
<?php
$query="SELECT * FROM designation_master";
$results=mysqli_query($con,$query);
foreach ($results as $dm_designation) {
?>
<option value="<?php echo $dm_designation["dm_designation"];?>";?>
<?php echo $dm_designation["dm_designation"];?></option>
<?php
}
?>
</select>
</div>
How to select only particular designations from the department and here is my code.
Please include JS library before script
<select class="form-control" id="selDepartment" name="selDepartment" autofocus="autofocus" onchange='xyz(this.value)'>
<script>
function xyz(temp){
$.ajax({
type:'post',
url: "abc.php",
data: "department="+temp
success: function(result){
$("#selDesignation").html(result);
}});
</script>
In abc.php
$department=$_POST['department'];
$html="<option value="-1" onmouseout="" ="">Select
Designation</option>";
$query="SELECT * FROM designation_master where department=$department";
$results=mysqli_query($con,$query);
foreach ($results as $dm_designation) {
$html=$html+" <option value=$dm_designation['dm_designation']> $dm_designation['dm_designation']</option>';
}
echo $html;
Related
What is the best way to return exactly the select option that the end user chose?
In my query for example when selecting the imoveis_tipo
Returns me all the data being HOUSES, APARTMENTS, TERRENOS;
How do I return only 1 purpose type?
If my question was not very clear, please comment here so I can adjust my question.
Here's an example showing what I'm trying to do, by selecting PURPOSE as SALE
PROPERTY TYPE as 2 BEDROOM APARTMENT
And the CITY as CAPÃO DA CANOA
Returns me all the records in the database and not just the ones I selected in the search.
This is my code showing the purpose, type and city:
<div class="col-md-12">
<label class="font-color-white font-weight-600 font-size-14 text-uppercase">Finalidade</label>
<select id="finalidade" name="finalidade" class="form-control" required>
<option value="0">
Selecione...
</option>
<?php
$sql5 = mysqli_query($link, "SELECT * FROM imoveis_finalidades WHERE status='S' ORDER BY nome");
while ($dados5 = mysqli_fetch_array($sql5)) {
?>
<option value='<?php echo $dados5['url'] ?>'>
<?php echo ($dados5['nome']); ?>
</option>
<?php
}
?>
</select>
</div>
<div class="col-md-12">
<label class="font-color-white font-weight-600 font-size-14 text-uppercase">Tipo de Imóvel</label>
<select id="tipo" name="tipo" class="form-control" required>
<option value="0">
Selecione...
</option>
<?php
$sql5 = mysqli_query($link, "SELECT * FROM imoveis_tipos WHERE status='S' ORDER BY nome ");
while ($dados5 = mysqli_fetch_array($sql5)) {
?>
<option value='<?php echo $dados5['url'] ?>'>
<?php echo ($dados5['nome']); ?>
</option>
<?php
}
?>
</select>
</div>
<div class="col-md-12">
<label class="font-color-white font-weight-600 font-size-14 text-uppercase">Cidade</label>
<select id="cidade" name="cidade" class="form-control" required>
<option value="0">
Selecione...
</option>
<?php
$sql6 = mysqli_query($link, "SELECT * FROM localidades_cidades WHERE status='S' ORDER BY nome");// or die (mysqli_error($link));
while ($dados6 = mysqli_fetch_array($sql6)) {
?>
<option value='<?php echo $dados6['url'] ?>'>
<?php echo ($dados6['nome']); ?>
</option>
<?php
}
?>
</select>
</div>
How can I fetch state from selected country in edit time?
This is for country:
<div class="col-md-6">
<select class="select2 form-control custom-select " style="width: 100%; height:36px;" name="country" onChange="getState(this.value);" id="country">
<option>Select Country</option>
<?php foreach($countries as $coun) {
foreach($ed as $edit) /*edit mode*/
{
if($edit->country_id==$coun->id)
{
?>
<option value="<?php echo $coun->id; ?>" selected="selected"> <?php echo $coun->name; ?> </option>
<?php
}}
?>
<option value="<?php echo $coun->id; ?>"> <?php echo $coun->name; ?> </option>
<?php
} ?>
</select>
</div>
This is for state:
<div class="col-md-6">
<select class="select2 form-control custom-select" style="width: 100%; height:36px;" name="state" id="state-list" onChange="getCity(this.value);">
<option>Select State</option>
</select>
</div>
function getState(val) {
$.ajax({
type: "POST",
url: "getState.php",
data:'country_id='+val,
success: function(data){
$("#state-list").html(data);
//getCity();
}
});
}
Try the code below:
function getState(val) {
$.ajax({
type: "POST",
url: "getState.php",
data:{country_id:val},
success: function(data){
$("#state-list").html(data);
//getCity();
}
});
}
When I select the all data of a product by it's product_id. and load product data in a form for an update. but here I cannot set selected drop-down list value in this case.
here, is my view
<form action="<?php //echo base_url().'admin_product/pro_update/'?>" method="post" class="form-horizontal">
<div class="control-group">
<label class="control-label">Title :</label>
<div class="controls">
<input type="text" class="span5 m-wrap" placeholder="Product title" name="title" value="<?php echo $item[0]['p_title']?>" />
<h6 style="color:red"><?php echo form_error('title')?></h6>
</div>
</div>
<div class="control-group">
<label class="control-label">Category :</label>
<div class="controls">
<select name="cat" id="cat">
<option value="">--Select Category--</option>
<?php foreach($cat as $c):?>
<option value="<?php echo $c->cat_id?>"><?php echo $c->cat_name;?>//selected set value here</option>
<?php endforeach;?>
</select>
<h6 style="color:red"><?php echo form_error('cat')?></h6>
</div>
</div>
here is my controller code,
public function pro_edit($id)
{
$p['item']=$this->ap->product_update($id);
$p['cat']=$this->ap->up_cat();
$p['color']=$this->ap->up_color();
$this->load->view('layouts/header',$p);
$this->load->view('admin/product_update',$p);
$this->load->view('layouts/footer',$p);
}
and model code,
public function product_update($id)
{
$this->db->select('p_title,p_description,p_category,p_stock_quantity,p_pprice,p_sprice,p_size_variant,cat_name,v_color,v_size,p_id');
$this->db->from('tbl_product');
$this->db->join('tbl_category','tbl_product.p_category=tbl_category.cat_id');
$this->db->join('tbl_variant','tbl_product.p_color_variant=tbl_variant.v_id');
$this->db->where('p_id',$id);
$query=$this->db->get();
return $query->result_array();
}
Assumptions
p_category is your category id
Code
<select name="cat" id="cat">
<option value="">--Select Category--</option>
<?php
foreach($cat as $c):?>
<option value="<?php echo $c->cat_id?>" <?php echo ($item[0]['p_category'] == $c->cat_id) ? 'selected' : '' ?>>
<?php echo $c->cat_name;?>
</option>
<?php
endforeach;
?>
</select>
Explain
Q - What this if do in above <?php echo ($item[0]['p_category'] == $c->cat_id) ? 'selected' : '' ?>
A - Assume $item[0]['p_category'] has value 2 so when ever $c->cat_id equesl to that value its print selected keyword
Ex:
<select>
<option value="1">aa</option>
<option value="2" selected>bb</option>
<option value="3">cc</option>
<option value="4">dd</option>
</select>
What I want is every time I have change the value from combobox (value from combobox is from database ) the input type will change too
here
<div class="col-md-12" >
<label>Charge</label>
<select id="name" name="name" class="form-control">
<?php
while ($reserve=mysqli_fetch_array($charge)) { ?>
<option value=" <?php echo $reserve['name']?>">
<?php echo $reserve['name']; ?>
</option><?php } ?>
</select>
</div>
<div class="col-md-12">
<br>
<label>Price</label>
<input type="number" class="form-control" id="Price" name="Price" disabled>
</div>
on database the name has a corresonding price for example chair has 200 price
what I want is every time I changed the combobox the value at inputtype will change too
I am not sure why you use ajax if you want to show option value . please check this code .
$('#name').change(function () {
var option_value = $(this).val();
$('#Price').val(option_value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<select id="name" name="name">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<div class="col-md-12">
<br>
<label>Price</label>
<input type="number" class="form-control" id="Price" name="Price" disabled>
</div>
</form>
so update select code using your this code
<select id="name" name="name" class="form-control">
<?php
while ($reserve=mysqli_fetch_array($charge)) { ?>
<option value=" <?php echo $reserve['name']?>">
<?php echo $reserve['name']; ?>
</option><?php } ?>
</select>
You need to add Jquery code to accomplish this task.
Your Html :
<div class="col-md-12" >
<label>Charge</label>
<select id="name" name="name" id="name" class="form-control">
<?php while ($reserve=mysqli_fetch_array($charge)) { ?>
<option value=" <?php echo $reserve['name']?>">
<?php echo $reserve['name']; ?>
</option><?php } ?>
</select>
</div>
<div class="col-md-12"><br>
<label>Price</label>
<input type="number" class="form-control" id="Price" name="Price" disabled>
</div>
Jquery Code :
$( document ).ready(function() {
$("#name").change(function () {
var value = this.value;
$('#Price').val(value);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<select id="name" name="name" class="form-control" onchange="callAjax(this)">
<?php
while ($reserve=mysqli_fetch_array($charge)) { ?>
<option value=" <?php echo $reserve['name']?>">
<?php echo $reserve['name']; ?>
</option><?php } ?>
</select>
<script>
function callAjax(val){
var selectedVal = val.value;
alert(selectedVal+" "+val)
$.ajax({
url: "<some_Url>", // Use PHP url from where you can get Data from Database
data: selectedVal,
success: function(result){
$("#Price").val(result);
}});
}
</script>
i have a problem in dependent dropdown and i am not professional in this issue
so the problem is when i choose the first one the another one doesnt get any value from database and i didnt know the solution
<?php
include 'header.php';
require 'connection.php';
?>
<div class="container">
<form id="contact" action="add_line.php" method="post">
<center> <h3>Add Line</h3></center>
<fieldset>
<?php
require 'connection.php';
$query = "select * from agents";
$result = mysqli_query($conn,$query);
?>
<div class="select">
<select class="agents-name " name="agents-name" autofocus tabindex="1">
<option selected="selected">--Select agent--</option>
<?php while ($row = mysqli_fetch_assoc($result)): ?>
<option value="<?php $row['id'];?>"><?php echo $row['name'];?></option>
<?php endwhile;?>
</select>
</div>
<div class="select" >
<select tabindex="1" name="sp_choosen" class="sp_choosen"
onChange="getState( this.value );" tabindex="2">
<option selected="selected">--Select service provider--</option>
<option value="CELLCOM">CELLCOM</option>
<option value="HoTMobile">HoTMobile</option>
<option value="Orange">Orange</option>
<option value="Pelphone">Pelphone</option>
<option value="Golan">Golan</option>
<option value="019">019</option>
</select>
</div>
<div class="select">
<select id="packet_select" name="packet_chossen" tabindex="3">
<option selected="selected">--Select packet--</option>
</select>
</div>
</fieldset>
<fieldset>
<input placeholder="customer name" type="text" tabindex="4"
name="customer_name" required >
</fieldset>
<fieldset>
<input placeholder="SIM_SERIAL" type="tel" tabindex="5" name="sim_serial"
required >
</fieldset>
<fieldset>
<input placeholder="phone_number" type="tel" tabindex="6" name="number"
required >
</fieldset>
<fieldset>
<label></label>
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit" tabindex="7" >Add
Available line</button>
</fieldset>
</form>
</div>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
function getState(val) {
$.ajax({
type: "POST",
url: "dropdown_add_line.php",
data:'sp='+val,
success: function(data){
$("#packet_select").html(data);
}
});
}
</script>
<?php
include 'footer.php';
?>
and this is the dropdown_add_line.php :
<?php
require_once("connectione.php");
$db_handle = new DBController();
if(!empty($_POST["sp"])) {
$sp=$_POST['sp'];
$query ="SELECT * FROM packets p WHERE sp LIKE '%$sp%'";
$results = $db_handle->runQuery($query);
?>
<option value="">--Select service provider--</option>
<?php
foreach($results as $packets) {
?>
<option value= "<?php echo $packets["id"]; ?>" ><?php echo $packets["packet"]; ?></option>
<?php
}
}
?>
and the table name is "packets" and the columns is "id","sp","packet"