php - I cant load second drop down menu, when the first one is changed. The second drop down is depended on the first one -
here code, having problem displaying values of second:
html: form, first drop down elements database query.
<form name="farmer" action="index.php" method="post"> <label> <span>chose land:</span> <select name="land" id="land"> <option value="">--land--</option> <?php $sql="select `city` `lands`"; $result =mysql_query($sql); while ($data=mysql_fetch_assoc($result)){ ?> <option value ="<?php echo $data['city'] ?>" ><?php echo $data['city'] ?></option> <?php } ?> </select> </label> <label> <span>region:</span> <select name="region" id="region"> <option value="">--region--</option> </select> </label> <input class="button4" type="submit" name="submit" value="submit" /> </form> js
jquery(document).ready(function() { jquery('#land').change(function() { jquery.post( 'getlist.json.php', { 'land': jquery('#land').val() }, function(data, textstatus) { jquery('#region').empty(); if(data != null) { jquery.each(data, function(index, value) { jquery('#region').append('<option value="' + value + '">' + value + '</option>'); }); } else { jquery('#region').append('<option value="">please select...</option>'); } }, 'json' ); }); }); getlist.json.php file - here make connection between region , land query(join).
<?php mysql_connect("localhost", "root", "") or die( "unable connect database"); mysql_select_db("farmer_fields") or die( "unable select database"); if($_post && $_post['land'] != "") { $sql="select region regions left join lands on regions.id_lands = lands.id"; $rows = array(); while ($data=mysql_fetch_assoc($sql)){ $rows['region'] = $data; } echo json_encode( $rows ); } ?>
no need of json here. can jquery , ajax
jquery:
function get_region(country_id) { if (country_id != 0) { $("#region_id").html("<option value='0'>select region</option>"); $("#region_id").prop("disabled", true); $.post("ajax/ajax.php", { country_id: country_id }, function (data) { var data_array = data.split(";"); var number_of_name = data_array.length - 1; var value; var text; var opt; var temp_array; (var = 0; < number_of_name; i++) { temp_array = data_array[i].split(","); value = temp_array[1]; //alert(value); text = temp_array[0]; opt = new option(text, value); $('#region_id').append(opt); $(opt).text(text); } $("#region_id").prop("disabled", false); }); } else { $("#region_id").html("<option value='0'>select region</option>"); $("#region_id").prop("disabled", true); } } ajax file ajax.php
if (isset($_post["country_id"])) { $country_id = $_post["country_id"]; $region_select = mysql_query("select * region country_id='$country_id'"); $region = ""; $region_id = ""; while ($region_row = mysql_fetch_array($region_select)) { $region = $region.$region_row["region"]. ",".$region_id.$region_row["id"]. ";"; } echo $region; } html of region select box:
<select name="region_id" id="region_id" disabled="disabled"> <option value="0">select region</option> </select> you may change mysql_query pdo security purpose mysql_query depriciated.
Comments
Post a Comment