json - jquery dynamic select list does not populate automaticaly in Codeigniter -
json - jquery dynamic select list does not populate automaticaly in Codeigniter -
my limits. can not create work: want create dynamic select list jquery , json populate info database see fetches info in firebug console not populate select box. here's view:
<div class="control-group"> <label class="control-label" for="inputpassword">grad</label> <div class="controls"> <select name="city" id="city" class="update"> <option value="">odaberi</option> <?php if (!empty($results)) { ?> <?php foreach($results $row) { ?> <option value="<?php echo $row->city_id; ?>"> <?php echo $row->city_name; ?> </option> <?php } ?> <?php } ?> </select> </div> </div> <div class="control-group"> <label class="control-label" for="inputpassword">deo grada</label> <div class="controls"> <select name="category" id="category" class="update" disabled="disabled"> <option value="">----</option> </select> </div> </div>
here controller phone call model:
function update_list(){ $this->marea->update_list();
and, model
function update_list(){ if (!empty($_get['id']) && !empty($_get['value'])) { $id = $_get['id']; $value = $_get['value']; seek { $objdb = new pdo('mysql:host=localhost;dbname=isport', 'root', ''); $objdb->exec('set character set utf8'); $sql = "select * `area` `city_id` = ?"; $statement = $objdb->prepare($sql); $statement->execute(array($value)); $list = $statement->fetchall(pdo::fetch_assoc); if (!empty($list)) { $out = array('<option value="">odaberi</option>'); foreach($list $row) { $out[] = '<option value="'.$row['area_id'].'">'.$row['area_name'].'</option>'; } echo json_encode(array('error' => false, 'list' => implode('', $out))); } else { echo json_encode(array('error' => true)); } } catch(pdoexception $e) { echo json_encode(array('error' => true)); } } else { echo json_encode(array('error' => true)); }
} here ajax:
var formobject = { run : function(obj) { if (obj.val() === '') { obj.nextall('.update').html('<option value="">----</option>').attr('disabled', true); } else { var id = obj.attr('id'); var v = obj.val(); jquery.getjson(site +'users/update_list', { id : id, value : v }, function(data) { if (!data.error) { obj.next('.update').html(data.list).removeattr('disabled'); return; } else { obj.nextall('.update').html('<option value="">----</option>').attr('disabled', true); } }); } }
}; $(function() {
$('.update').live('change', function() { formobject.run($(this)); });
});
i have no thought how create work. please help!
your issue utilize of jquery next
function,
given jquery object represents set of dom elements, .next() method allows search through next sibling of these elements in dom tree , build new jquery object matching elements.
your select box not in same set, next returning undefined , nil getting updated. if need utilize code 1 time specific spot, can add together class or id sec selector specify it.
var formobject = { run : function(obj) { if (obj.val() === '') { obj.nextall('.update').html('<option value="">----</option>').attr('disabled', true); } else { var id = obj.attr('id'); var v = obj.val(); jquery.getjson(site +'users/update_list', { id : id, value : v }, function(data) { if(!data.error) { $('#secondselect').html(data.list).removeattr('disabled'); } else { $('#secondselect').html('<option value="">----</option>').attr('disabled', true); } }); } } };
jquery json codeigniter
Comments
Post a Comment