javascript - Populate select menu onChange -
i have bootstrap select menu want populate onchange select menu. think have problems returning data php.
select menu:
<div class="col-md-6" style="width:100%;margin-bottom: 10px;"> <div class="input-group" style="width:100%;"> <span style="width:50%;" class="input-group-addon" id="basic-addon1">municipality *</span> <select class="selectpicker" name="object_municipality" id="object_municipality"> <option value="0" selected="selected" >municipality *</option> </select> </div> </div>
javascript function (called onchange select menu) populate select menu:
function populate_obcina(value) { if (window.xmlhttprequest) { xmlhttp=new xmlhttprequest(); } else { xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.getelementbyid("object_municipality").innerhtml=xmlhttp.responsetext; } } xmlhttp.open("get","get_municipality.php?q="+value,true); xmlhttp.send(); }
my get_municipality.php file:
<?php require_once 'functions.php'; $conn = dbconnect(); $q =($_get['q']); $municipality = mysqli_query($conn, "select id,municipality municipalities `region`='".$q."'") or die(file_put_contents('error_querry.txt', print_r("error: " . mysqli_error($conn), true))); while ($row = mysqli_fetch_array($municipality, mysqli_assoc)) { $fp = fopen("while_loop.txt", "a") or die("couldn't open log file writing."); fwrite($fp, php_eol .'<option value="'.$row['id'].'">'.$row['municipality'].'</option>'); fflush($fp); fclose($fp); //in while_loop.txt lines this: //<option value="1">chicago</option> //so guess problem way returning results echo '<option value="'.$row['id'].'">'.$row['municipality'].'</option>'; } mysqli_close($conn); ?>
this return get:
<option value="1">chicago</option><option value="2">la</option><option value="3">california</option>
i did kind of job, did differently since had able populate many kinds of selects, on events, pre-chosen data or not, ... jquery, bootstrap, & on...
select html :
<div class="col-md-6" style="width:100%;margin-bottom: 10px;"> <div class="input-group" style="width:100%;"> <span style="width:50%;" class="input-group-addon" id="basic-addon1">municipality *</span> <select class="selectpicker" name="object_municipality" id="object_municipality"> <option value="0" selected="selected" >municipality *</option> </select> </div> </div>
javascript/jquery populate "class", make file called populatelist.js :
function populatelist(){ } populatelist.municipality = function(element,choice){ $(document).ready(function(){ $.ajax({ type : 'post', url : './getmunicipalitieschoice.php', data : {'choice':choice}, datatype : 'json', error : function(response){ alert('something went wrong'); }, success : function(response){ element.html(response); } }); }); };
jquery on change event :
$(document).on('change','#listfiringtheevent',function(){ //call populate function here populatelist.municipality('#object_municipality',$(this).val()); });
php getmunicipalitieschoice.php :
<?php require_once 'functions.php'; if(isset($_post['choice'])){ $conn = dbconnect(); $q = $_post['choice']; $result = ''; $municipality = mysqli_query($conn, "select id,municipality municipalities `region`='".$q."'") or die(file_put_contents('error_querry.txt', print_r("error: " . mysqli_error($conn), true))); while ($row = mysqli_fetch_array($municipality, mysqli_assoc)) { $fp = fopen("while_loop.txt", "a") or die("couldn't open log file writing."); fwrite($fp, php_eol .'<option value="'.$row['id'].'">'.$row['municipality'].'</option>'); fflush($fp); fclose($fp); $result.='<option value="'.$row['id'].'">'.$row['municipality'].'</option>'; } mysqli_close($conn); echo json_encode($result); }else{ //if you're here, that's because file has been called "invalid" choice (not set) } ?>
now, said, if have other lists fill, add functions in populatelist.js file this, example, function fills list municipalities, not depending on choice :
populatelist.municipalities = function(element){ $(document).ready(function(){ $.ajax({ type : 'post', url : './getmunicipalities.php', datatype : 'json', error : function(response){}, success : function(response){ element.html(response); } }); }); };
or example fill "cities" list when chose "municipality" :
populatelist.citiesonmunicipality= function(element,municipality){ $(document).ready(function(){ $.ajax({ type : 'post', url : './getcitiesonmunicipality.php', data : {'municipality':municipality}, datatype : 'json', error : function(response){}, success : function(response){ element.html(response); } }); }); };
in example here, assume html , php code "good".
but (for php) have use prepared statements...
hope helps!
Comments
Post a Comment