value selected in dropdown when back to this page php through ajax -
value selected in dropdown when back to this page php through ajax -
there 2 dropdown . 1 loaded province , other loaded district on alter province dropdown. how show selected items in both dropdown when error occur or other reason redirect test.php page. ?? when page province dropdown selected district not selected..
test.php
<?php include "connection.php"; $msg = ""; if(isset($_request['msg']) && !empty($_request['msg'])) { $msg = $_request['msg']; } if ( isset($_session['province']) && !empty($_session['province']) ) { $province = $_session['province']; } # province $sqlprovince = "select * tbl_province"; $resprovince = mysql_query($sqlprovince) or die(mysql_error()); ?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>untitled document</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.6/jquery.validate.min.js"></script> <script type="text/javascript"> function showdistrict(province){ $("#district").html(' '); // city $.ajax({ url: "js/ajaxdistrict.php", type: 'post', //datatype: 'json', data: {'province': province}, //datatype: 'json', cache: false, success: function(data){ $("#district").html(data); } }); } </script> </head> <body> <?php echo $msg; ?> <form id="myfrom" action="showtest.php" method="get"> <p> `enter code here`<label>province:<small>*</small></label> <select name="province" id="province" onchange="showdistrict(this.value);" required> <option value="">-select-</option> <?php if(mysql_num_rows($resprovince) > 0) { while($rowprovince = mysql_fetch_array($resprovince)){ ?> <option value="<?php echo $rowprovince['name'];?>"<?php if( $province == $rowprovince['name'] ) { echo "selected='selected'";} ?>"><?php echo $rowprovince['name'];?></option> <?php } } ?> </select> </p> <p> <label>postal city district:<small>*</small></label> <select name="district" id="district" > <option value="">-select-</option> <option value="<?php echo $_session['district']['city_district'];?>"><?php echo $_session['district']['city_district'];?></option> </select> </p> <input name="submit" type="submit" /> </form> </body> </html> ajaxdistrict.php
<?php include "../connection.php"; $res = ""; $province = $_request['province']; $_session['province'] = $province; $query="select * districts province = '".$province."' "; $result=mysql_query($query) or die(mysql_error()); $str = ""; //$_session['district'] = array(); if(mysql_num_rows($result)>0) { while($row = mysql_fetch_array($result)) { $_session['district']['city_district'] = trim($row['city_district']); $city_district = trim($row['city_district']); if($_session['district']['city_district'] == $city_district) { $str = "selected='selected'";} $res .= "<option value=\"".$city_district."\" ".$str." >".$city_district."</option>"; } } else{ $res .= "<option value=''>-select-</option>"; } echo $res; ?>
you have maintain value of province , district in session can populate accordingly or can utilize cookie same.
php ajax
Comments
Post a Comment