php - How to refresh dropdown without page refresh? -
i having following difficulties:
i fetching value of dropdown mysql , want information displayed in dropdown list.
see this:
<select id="location" name="location" class='form-control'> <option value="0">select location</option> <?php $query = mysql_query("select cityname city"); while($row = mysql_fetch_assoc($query)) { echo '<option value="'.$row['cityname'].'">'.$row['cityname']. '</option>'; } ?> </select>
by using code populating values database dropdown list, need refresh page values displayed.
thank you.
use jquery ajax
yourfile.php
<select id="location" onchange="getstate(this.value)" name="location" class='form-control'> <option value="0">select location</option> <?php $query = mysql_query("select * city"); while($row = mysql_fetch_assoc($query)) { echo '<option value="'.$row['cityid'].'">'.$row['cityname']. '</option>'; } ?> </select> <select id="state"> </select>
jquery script
function getstate(city_id) { var html = $.ajax({ type: "post", url: "path/to/ajax/my_ajax.php", data: "city_id=" +city_id, async: false }).responsetext; if(html){ $("#state").html(html); } }
ajax.php
$query = mysql_query("select * state city_id=".$_request['city_id']); while($row = mysql_fetch_assoc($query)) { echo '<option value="'.$row['state_id'].'">'.$row['state_name']. '</option>'; }
Comments
Post a Comment