I have two pages add_admin.php and ajax_admin.php when i select name in dropdown it display FIRSTNAME, MIDDLENAME and LASTNAME in TEXTBOXES.
HERE is My Code:
add_admin.php
<!-- Department -->
<div class="col-xs-12 col-sm-12 col-md-12" >
<div class="form-group">
<select id="faculty_name" name="faculty_name" class="form-control" onchange='fetch_select(this.value)' required>
<option selected="selected" disabled="disabled">Please Select Faculty</option>
<?php
$query = mysql_query("select * from faculty_details");
while($row = mysql_fetch_array($query))
{
?>
<option value="<?php echo $row['FACULTY_ID'];?>"><?php echo $row['FIRSTNAME']." ".$row['MIDDLENAME']." ".$row['LASTNAME'];?></option>
<?php
}
?>
</select>
</div>
</div>
<!-- First Name -->
<div class="col-xs-12 col-sm-4 col-md-4">
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-fw fa-user"></i></div>
<input type="text" id="fname" name="fname" class="form-control input-md" placeholder="First Name" value="">
</div>
</div>
</div>
other text box are there contaisn middlename, lastname
Ajax Code
function fetch_select(val)
{
$.ajax
({
type: 'post',
url: 'ajax_admin.php',
data:
{
get_option:val
},
success: function (response)
{
$('#fname').val(response);
}
});
ajax_admin.php
<?php
if(isset($_POST['get_option']))
{
$state = $_POST['get_option'];
$find=mysql_query("select * from faculty_details where FACULTY_ID=$state");
while($row=mysql_fetch_array($find))
{
echo "$row[FIRSTNAME]";
}
}
?>
Get the result as json from the server so that you can easily use it in jQuery:
ajax-admin.php
if(isset($_POST['get_option']))
{
$state = $_POST['get_option'];
$row1=array();
$find=mysql_query("select firstname,middlename,lastname from faculty_details where FACULTY_ID=$state");
while($row=mysql_fetch_array($find))
{
$row1[]=$row;
}
die(json_encode($row1));
}
Ajax Code
function fetch_val(val) {
$.ajax({
url:"ajax-admin.php",
type:"POST",
data:{"get_option":val},
dataType:"JSON",
success:function(data){
$('#fname').val((data[0].firstname));
$('#mname').val((data[0].middlename));
$('#lname').val((data[0].lastname));
}
});
}
Related
i want to show checkbox looping from db, if i try to select data id_kendaraan in field then show the data from that id.
this my view code:
<div class="modal fade" id="tambah" tabindex="-1" role="dialog" aria-labelledby="largeModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
<h3 class="modal-title" id="myModalLabel">Tambah Pemeliharaan</h3>
</div>
<form class="form-horizontal" method="post" action="<?php echo base_url()?>Data/Pemeliharaan/Tambah">
<div class="modal-body">
<div class="form-group">
<label class="control-label col-xs-3">Nama Kendaraan</label>
<div class="col-xs-8">
<select class="form-control" name="id_kendaraan" id="id_kendaraan" required>
<option>Pilih Kendaraan</option>
<?php
if(!empty($kendaraan_)) {
foreach ($kendaraan_ as $isi) {
?>
<option value="<?php echo $isi['id_kendaraan']?>"><?php echo $isi['nama']?> - <?php echo $isi['platno']?></option>
<?php }} ?>
</select>
</div>
</div>
<?php
$index =0;
foreach ($subkriteria_ as $kr_key => $kriteria) {
?>
<div class="form-group">
<label class="control-label col-xs-3"><?php echo $kriteria['nama_kriteria'] ?></label>
<div class="col-xs-8">
<input type="hidden" name="status_pemeliharaan" value="1" class="form-control">
<input type="hidden" name="id_kriteria[]" value="<?php echo $kriteria['id_kriteria'] ?>" class="form-control">
<?php if(!empty($kriteria['sub'])) { ?>
<select class="form-control" name="isi_kriteria[]" required>
<option>Pilih Sub Kriteria</option>
<?php $no = 1; foreach ($kriteria['sub'] as $data) { ?>
<option value="<?php echo $data['value'] ?>"><?php echo $data['namasubkriteria'] ?> - <?php echo $data['value'] ?></option>
<?php $no++; } ?>
</select>
<?php } else if($kriteria['link']=='tahun_beli') { ?>
<input name="isi_kriteria[]" value="" class="form-control" type="text" placeholder="Isi Kriteria..." readonly required>
<?php } else { ?>
<input name="isi_kriteria[]" value="" class="form-control" type="number" placeholder="Isi Kriteria..." required>
<?php } ?>
</div>
</div>
<?php } ?>
<div class="form-group">
<label class="control-label col-xs-3">tes</label>
<div class="col-xs-8">
<input type="checkbox" name="id_sparepart[]" value=""> //my trouble
<input type="text" name="sparepart" value=""> //my trouble
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Tutup</button>
<button class="btn btn-info">Submit</button>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#id_kendaraan').on('input',function(){
var id_kendaraan=$(this).val();
$.ajax({
type : "POST",
url : "<?php echo base_url('Data/Pemeliharaan/GetKendaraanById')?>",
dataType : "JSON",
data : {id_kendaraan: id_kendaraan},
cache:false,
success: function(data){
$.each(data,function(id_kendaraan, tahun_beli, jenis){
$('[name="isi_kriteria[]"]').val(data.tahun_beli);
$('[name="id_jenis"]').val(data.id_jenis);
$('[name="id_sparepart[]"]').val(data.id_sparepart); //my trouble
$('[name="sparepart"]').val(data.sparepart); //my trouble
});
}
});
return false;
});
});
</script>
on this code, if I select a kendaraan from the select form then data appears according to the selected kendaraan.
it is working, but on the checkbox not show data sparepart for looping from database.
this my controller:
function GetKendaraanById(){
$id_kendaraan=$this->input->post('id_kendaraan');
$data=$this->PemeliharaanModel->GetById($id_kendaraan); ;
echo json_encode($data);
}
i get data from view $id_kendaraan for my parameter.
this my model:
function GetById($id_kendaraan){
$this->db->select('*');
$this->db->from('tb_kendaraan');
$this->db->join('tb_sparepart', 'tb_sparepart.id_jenis = tb_kendaraan.id_jenis');
$this->db->where('tb_kendaraan.id_kendaraan',$id_kendaraan);
$query = $this->db->get();
if($query->num_rows() > 0)
{
foreach ($query->result() as $data) {
$hasil=array(
'tahun_beli' => $data->tahun_beli,
'id_jenis' => $data->id_jenis,
'id_sparepart' => $data->id_sparepart,
'sparepart' => $data->sparepart,
);
}
}
return $hasil;
}
field tahun_beli, id jenis is success to showing in the view.
but the checkbox from sparepart is not looping just show one data.
after selecting the nama kendaraan the checkbox should appear according to the type of kendaraan, data circled in the image should appear more checkbox
thx for helping me
You should separate the query on your model because it has a different format to show (I assume you want to get one kendaraan data with multiple sparepart data) :
function GetById($id_kendaraan){
$this->db->select('*');
$this->db->from('tb_kendaraan');
$this->db->where('tb_kendaraan.id_kendaraan',$id_kendaraan);
$query_kendaraan = $this->db->get();
$hasil = false;
if($query_kendaraan->num_rows() > 0)
{
$data_kendaraan = $query_kendaraan->row();
$hasil = [
'tahun_beli' => $data_kendaraan->tahun_beli,
'id_jenis' => $data_kendaraan->id_jenis,
'spareparts' => [] // empty array, to be filled with spareparts data
];
$this->db->select('*');
$this->db->from('tb_sparepart');
$this->db->where('tb_sparepart.id_jenis', $data_kendaraan->id_jenis);
$query_sparepart = $this->db->get();
foreach ($query_sparepart->result() as $data_sparepart) {
$spareparts = [
'id_sparepart' => $data_sparepart->id_sparepart,
'sparepart' => $data_sparepart->sparepart,
];
array_push($hasil['spareparts'], $spareparts);
}
}
return $hasil;
}
Then on the html side, try to modify the codes like this :
<div class="modal fade" id="tambah" tabindex="-1" role="dialog" aria-labelledby="largeModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
<h3 class="modal-title" id="myModalLabel">Tambah Pemeliharaan</h3>
</div>
<form class="form-horizontal" method="post" action="<?php echo base_url()?>Data/Pemeliharaan/Tambah">
<div class="modal-body">
<div class="form-group">
<label class="control-label col-xs-3">Nama Kendaraan</label>
<div class="col-xs-8">
<select class="form-control" name="id_kendaraan" id="id_kendaraan" required>
<option>Pilih Kendaraan</option>
<?php
if(!empty($kendaraan_)) {
foreach ($kendaraan_ as $isi) {
?>
<option value="<?php echo $isi['id_kendaraan']?>"><?php echo $isi['nama']?> - <?php echo $isi['platno']?></option>
<?php }} ?>
</select>
</div>
</div>
<?php
$index =0;
foreach ($subkriteria_ as $kr_key => $kriteria) {
?>
<div class="form-group">
<label class="control-label col-xs-3"><?php echo $kriteria['nama_kriteria'] ?></label>
<div class="col-xs-8">
<input type="hidden" name="status_pemeliharaan" value="1" class="form-control">
<input type="hidden" name="id_kriteria[]" value="<?php echo $kriteria['id_kriteria'] ?>" class="form-control">
<?php if(!empty($kriteria['sub'])) { ?>
<select class="form-control" name="isi_kriteria[]" required>
<option>Pilih Sub Kriteria</option>
<?php $no = 1; foreach ($kriteria['sub'] as $data) { ?>
<option value="<?php echo $data['value'] ?>"><?php echo $data['namasubkriteria'] ?> - <?php echo $data['value'] ?></option>
<?php $no++; } ?>
</select>
<?php } else if($kriteria['link']=='tahun_beli') { ?>
<input name="isi_kriteria[]" value="" class="form-control" type="text" placeholder="Isi Kriteria..." readonly required>
<?php } else { ?>
<input name="isi_kriteria[]" value="" class="form-control" type="number" placeholder="Isi Kriteria..." required>
<?php } ?>
</div>
</div>
<?php } ?>
<div class="form-group">
<label class="control-label col-xs-3">tes</label>
<div class="col-xs-8">
<ul id="sparepartList" style="list-style-type: none; padding: 0;"></ul>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Tutup</button>
<button class="btn btn-info">Submit</button>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#id_kendaraan').on('input',function(){
var id_kendaraan=$(this).val();
$.ajax({
type : "POST",
url : "<?php echo base_url('Data/Pemeliharaan/GetKendaraanById')?>",
dataType : "JSON",
data : {id_kendaraan: id_kendaraan},
cache:false,
success: function(data){
$('#sparepartList').empty(); // clear all sparepart checkboxes
$.each(data, function(id_kendaraan, tahun_beli, jenis){
$('[name="isi_kriteria[]"]').val(data.tahun_beli);
$('[name="id_jenis"]').val(data.id_jenis);
});
$.each(data.spareparts, function (key, value) {
let li = $('<li><input type="checkbox" name="id_sparepart[]" value="' + value.id_sparepart + '" />' +
'<input type="text" name="sparepart" value="' + value.sparepart + '" /></li>');
$('#sparepartList').append(li);
});
}
});
return false;
});
});
</script>
Notice that I changed the sparepart html inputs to an unordered list element, then loop the sparepart data retrieved from the ajax response inside it.
in Your model
$hasil=array(
'tahun_beli' => $data->tahun_beli,
'id_jenis' => $data->id_jenis,
'id_sparepart' => $data->id_sparepart,
'sparepart' => $data->sparepart,
);
U should Store data in $hasil by using []
$hasil[]=array(
'tahun_beli' => $data->tahun_beli,
'id_jenis' => $data->id_jenis,
'id_sparepart' => $data->id_sparepart,
'sparepart' => $data->sparepart,
);
my code works fine for the first list. But not work even other list if has more list. Could you please find out what happened with my code.
if I click on signOut it will be add to database and remove form the page with signOut value only. First Div list work,But other will not response anything.
Thank You
Here is my code:
Java Script Code:
$(function() {
$("#add").click(function(){
//Save the link in a variable called element
var element = $(this);
//Find the id of the link that was clicked
var del_id = element.attr("dataid");
var outime = $(this).parents("#list").find("#outtime").val();
//Built a url to send
var info = 'id=' + del_id+ '&singout=' + outime;
$.ajax({
type: "POST",
url: "signOut.php",
data: info,
success: function(){
}
});
// After success
$(this).parents("#list").animate({ backgroundColor: "#fbc7c7" }, "fast")
.animate({ opacity: "hide" }, "slow");
});
});
HTML COde:
<?php
$comments = runQuery($conn, "SELECT * FROM `civ_in_out` WHERE `out_time` = 'null'");
//print_r($comments) ;
if(!empty($comments)) {
foreach($comments as $k=>$v) {
?>
<div id="list">
<div class="form-group">
<div class="form-row">
<div class="col-md-3">
<input class="form-control" type="text" name="name" value="<?php echo $comments[$k]['name']; ?>" disabled>
</div>
<div class="col-md-3">
<input class="form-control" type="time" name="signIn" value="<?php echo $comments[$k]['in_time']; ?>" disabled>
</div>
<div class="col-md-3">
<input class="form-control" id="outtime" type="time" name="singOut">
<input class="form-control" id="id" type="hidden" name="id" value="<?php echo $comments[$k]['id']; ?>">
</div>
<div class="col-md-3">
<a class="btn btn-primary btn-block" dataid="<?php echo $comments[$k]['id']; ?>" id="add" >Sign Out</a>
</div>
</div>
</div>
</div>
<?php
} }
?>
signOut.php
include 'connect.php';
$data=$_POST['serialize'];
echo $id = $data['id'];
echo $outtime = $data['singout'];
$sql = "UPDATE `civ_in_out` SET `out_time`='$outtime' WHERE id = '$id'";
mysqli_query($conn, $sql);
The problem is that you are repeating the list id and all the other element ids, so the jQuery call and event binding will always refer to the first id found on the HTML.
Id’s must be unique on you html.
You can change your code to use a class name for example:
$(function() {
$(".add").click(function(){
//Save the link in a variable called element
var element = $(this);
//Find the id of the link that was clicked
var del_id = element.attr("dataid");
var outime = $(this).parents(".list").find(".outtime").val();
//Built a url to send
var info = 'id=' + del_id+ '&singout=' + outime;
$.ajax({
type: "POST",
url: "signOut.php",
data: info,
success: function(){}
});
// After success
$(this).parents(".list").animate({ backgroundColor: "#fbc7c7" }, "fast").animate({ opacity: "hide" }, "slow");
});
});
HTML/PHP:
<?php
$comments = runQuery($conn, "SELECT * FROM `civ_in_out` WHERE `out_time` = 'null'");
//print_r($comments) ;
if(!empty($comments)) {
foreach($comments as $k=>$v) {
?>
<div class="list">
<div class="form-group">
<div class="form-row">
<div class="col-md-3">
<input class="form-control" type="text" name="name[]" value="<?php echo $comments[$k]['name']; ?>" disabled>
</div>
<div class="col-md-3">
<input class="form-control" type="time" name="signIn[]" value="<?php echo $comments[$k]['in_time']; ?>" disabled>
</div>
<div class="col-md-3">
<input class="form-control outtime" id="outtime-<?=$k?>" type="time" name="singOut[]">
<input class="form-control" id="id-<?=$k?>" type="hidden" name="id[]" value="<?php echo $comments[$k]['id']; ?>">
</div>
<div class="col-md-3">
<a class="btn btn-primary btn-block add" dataid="<?php echo $comments[$k]['id']; ?>>Sign Out</a>
</div>
</div>
</div>
</div>
<?php
} }
?>
You can change $("#add").click(function() to $(document).on('click', '#add', function()
If I select item name then automatically change price in unit price text box.
But the problem is if I edit the unit price and change again item name, price not change according to item name. Last edited value of unit price display for every item name. What is the solution for this.
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Item Name</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<select class="form-control select2" class="form-control" onchange="fetch_select(this.value);">
<option selected="selected">Select a Category</option>
<?php
$select = mysqli_query($con, "select ItemName from bincard group by ItemName");
while ($row = $select->fetch_assoc()){
echo "
<option value=".$row['ItemName'].">".$row['ItemName']."</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Unit Price</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<textarea rows="1" cols="1" style="resize: none;" class="form-control select2"
class="form-control" id="UnitPrice" name="UnitPrice"></textarea>
</div>
</div>
<script type="text/javascript">
function fetch_select(val) {
$.ajax({
type: 'post',
url: 'fetch_data.php',
data: {
get_option: val
},
success: function(response) {
document.getElementById("UnitPrice").innerHTML = response;
}
});
}
</script>
****** fetch_data.php ******
<?php
include ("common/DBCon.php");
if (isset($_POST['get_option']))
{
$ECatagory = $_POST['get_option'];
$find = mysqli_query($con, "select SellingPrice from bincard where ItemName='$ECatagory'");
while ($row = $find->fetch_assoc())
{
echo "" . $row['SellingPrice'] . "";
}
exit;
}
?>
I think you must set id to select box and try:
$(function(){
$("#your select id").change(function(){
fetch_select($(this).val());
});
});
I want to update my table using bootstrap dialog , but I when I retrieve my datatable from server side and embed php code into my modal to show the table data into the modal, and choose what the user wants to update in that form. it is showing the data from every row of my table into the modal , how can I fix it?
form_modal
<form id="product_update">
<div class="row">
<div class="form-group">
<?php foreach ($product as $value): ?>
<div class="col-xs-12">
<label for="description">Name: </label>
<input type="text" class="form-control" id="description" name="description" title="product description" value="<?php echo $value['descripcion']; ?>" required>
<div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-xs-8 col-sm-6">
<label for="cost_price">Cost Price:</label>
<div class="input-group"> <span class="input-group-addon">$</span>
<input type="text" class="form-control input-group-lg reg_name" id="cost_price" name="cost_price" title="cost_price" placeholder="Last name" value="<?php echo $value['precio_compra']; ?>" required>
</div>
</div>
<div class="col-xs-8 col-sm-6">
<label for="selling_price">Selling price: </label>
<input type="text" class="form-control input-group-lg reg_name" id="selling_price" name="selling_price" title="selling_price" placeholder="Last name" value="<?php echo $value['precio_venta']; ?>" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-xs-8 col-sm-6">
<label for="wprice">Wholeprice: </label>
<input type="text" class="form-control" id="wprice" name="wprice" title="wprice" value="<?php echo $value['precio_mayoreo']; ?>" required>
</div>
<div class="col-xs-8 col-sm-6">
<label for="min_stock">Min stock: </label>
<input type="text" class="form-control" id="min_stock" name="min_stock" title="min_stock" value="<?php echo $value['existencia_minima']; ?>" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-xs-8 col-sm-6">
<label for="stock">Stock: </label>
<input type="text" class="form-control" id="stock" name="stock" title="stock" value="<?php echo $value['existencia']; ?>" required>
</div>
<div class="col-xs-8 col-sm-6">
<label for="max_stock">Max stock: </label>
<input type="text" class="form-control" id="max_stock" name="max_stock" title="max_stock" value="<?php echo $value['existencia_maxima']; ?>" required>
</div>
</div>
</div>
<?php endforeach ?>
<div class="row">
<div class="form-group">
<div class="col-xs-8 col-sm-6">
<label for="provider">Provider: </label>
<select name="select-provider" id="select-provider">
<option value="0">Select a provider</option>
<?php foreach ($data as $value): ?>
<option value="<?php echo $value['id']; ?>"><?php echo $value['first_name'].' '.$value['last_name'] ?></option>
<?php endforeach ?>
</select>
</div>
</div>
</div>
</form>
modal js
$('#example tbody').on('click', 'a', function(event) {
event.preventDefault();
var data = table.row($(this).parents('tr')).data();
hash = data[0];
$("#product_update").validate();
BootstrapDialog.show({
type: BootstrapDialog.TYPE_WARNING,
message: function(dialog) {
var $message = $('<div></div>');
var pageToLoad = dialog.getData('pageToLoad');
$message.load(pageToLoad);
return $message;
},
data: {
'pageToLoad': URL_GET_VIEW_PRODUCT_UPDATE
},
closable: false,
buttons: [{
id: 'btn-ok',
cssClass: 'btn-primary',
icon: 'glyphicon glyphicon-send',
label: ' Save',
action: function(e) {
var description = $('#description').val();
var description = $('#description').val();
var cost_price = $('#cost_price').val();
var selling_price = $('#selling_price').val();
var wprice = $('#wprice').val();
var min_stock = $('#min_stock').val();
var stock = $('#stock').val();
var max_stock = $('#max_stock').val();
var provider_id = $('#select_provider').val();
if ($("#product_update").valid()) {
$.ajax({
url: URL_GET_UPDATE_PRODUCT,
type: 'POST',
data: { hash: hash, provider_id: provider_id, description: description, cost_price: cost_price, selling_price: selling_price, wprice: wprice, min_stock: min_stock, stock: stock, max_stock: max_stock },
success: function(data) {
console.log(data);
if (data.msg == 'successfully updated') {
$('#product_update')[0].reset();
table.ajax.reload();
} else if (data.min_stock == 'el stock no puede ser mayor al min') {
BootstrapDialog.show({
type: BootstrapDialog.TYPE_DANGER,
message: 'el stock no puede ser mayor al min'
});
}
}
});
}
}
},{
id: 'btn-cancel',
cssClass: 'btn-danger',
icon: 'glyphicon glyphicon-remove',
label: ' Cancel',
action: function(e) {
e.close();
}
}]
});
});
model product
public function datatable(){
$this->db->select('hash_id,codigo,descripcion,precio_compra,precio_venta,precio_mayoreo,existencia_minima,existencia,existencia_maxima,storelte_articulos.status');
$this->db->from('storelte_articulos');
$query = $this->db->get();
return $query->result_array();
}
public function isExistsProduct($data){
$this->db->select('descripcion');
$this->db->from('storelte_articulos');
$this->db->where('descripcion',$data['descripcion']);
$query = $this->db->get();
return $query->num_rows() == 0 ? false : true;
}
public function addProduct($data){
$query = 'UPDATE storelte_articulos SET hash_id = MD5(id) WHERE id = LAST_INSERT_ID()';
$this->db->insert('storelte_articulos',$data);
$this->db->query($query);
}
public function updateProduct($data) {
$this->db->where('md5(id)',$this->input->post('hash'));
$this->db->update('storelte_articulos',$data);
}
public function get_product() {
$this->db->select('codigo,descripcion,precio_compra,precio_venta,precio_mayoreo,existencia_minima,existencia_maxima,existencia');
$this->db->from('storelte_articulos');
$this->db->where('md5(id)', $this->input->post('hash'));
$query = $this->db->get();
return $query->result_array();
}
modal img
I suspect your query is not targeting the single row that you intend to access.
If you are not querying on your table Primary/Unique Key, you should add LIMIT 1 --but really you should be using the PK.
Also, you seem to be looping through all of the results from the query.
You'll need to post more of your code, because I can only make assumptions from what you've provided.
Why are you putting the Warning element at the top?
Where did you copy/paste your scripts from?
public function datatable(){
$this->db->select('hash_id,codigo,descripcion,precio_compra,precio_venta,precio_mayoreo,existencia_minima,existencia,existencia_maxima,storelte_articulos.status');
You will need some sort of $this->db->where() statement here that references the appropriate row id.
like: $this->db->where('id',$this->input->post('id'));
$this->db->from('storelte_articulos');
$query = $this->db->get();
return $query->result_array();
}
$this->db->select('hash_id,codigo,descripcion,precio_compra,precio_venta,precio_mayoreo,existencia_minima,existencia,existencia_maxima,storelte_articulos.status');
You should add WHERE clause here. Basically you are selecting all rows if you don't add any kind of filtering in that clause. For example:
$this->db->select('hash_id,codigo,descripcion,precio_compra,precio_venta,precio_mayoreo,existencia_minima,existencia,existencia_maxima,storelte_articulos.status' WHERE hash_id='YOURHASHID')
view like this:
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="inputEmail3" class="col-sm-4 control-label">Brand Name</label>
<div class="col-sm-8">
<select class="form-control" id="brand_mod" name="brand_name" required>
<option value="" selected disabled="disabled">Select Brand Here</option>
<?php foreach($b->result() as $row) { ?>
<option value="<?php echo $row->id; ?>"> <?php echo $row->brand_name; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">Model</label>
<div class="col-sm-8">
<select name="model_number" class="form-control" id="models">
<option value="">--Select model --</option>
</select>
</div>
</div>
</div>
</div>
in brand name dropdown brand names are properly visible below that there is my model name dropdown... i want when i click on any brand name its models get displayed on model dropdown
then my ajax call is
<script>
$('#brand_mod').change(function(){
var bbb = $('#brand_mod').val();
//alert(bbb);
$.ajax({
type: "GET",
url: '#Url.Action("<?php echo base_url(); ?>User/get_models", "Application")',
contentType: "application/json; charset=utf-8",
data: { brand_id : $('#brand_mod').val() },
dataType: "json",
cache: false,
success: function(data)
{
alert(data);
alert('Success');
return false;
},
error:function()
{
alert('error');
return false;
}
});
});
</script>
then my controller is like this to which ajax call is made
public function get_models(){
$id = $this->input->post('brand_id');
$this->load->model("user/User_model");
$result1 = $this->User_model->all_models($id);
//$data = array();
'<option value=""></option>'
foreach($result1 as $row)
{
'<option value="'.$row->id'">'.$row->model_name'</option>'
}
}
and my model is
public function all_models($id)
{
$this->db->where('brand_id',$id);
$query = $this->db->get('wc_models');
return $query->result_array();
}
but i am getting error alert.... something is wrong with code... i have tried my best... please help...