to pass the html combobox value to php in same file without submit -
i want change combobox2 entry based on selection in combobox1.. want retrieve entries of combobox2 database , need in php. need pass selected value in combobox1 php php variable. tried lot... not yet got output. here code
degree type<select id="select3" name="deg"> <option value="ug">ug</option> <option value="pg">pg</option> </select> department name<?php $host="localhost" ; $mysql_db="db" ; $mysql_u="root" ; $mysql_p="" ; mysql_connect( "$host", "$mysql_u", "$mysql_p"); mysql_select_db( "$mysql_db"); $sel="select dept_name dept_mast deg_type='???' "; $val=mysql_query($sel); $selectbox='<select name=\'userst\'>'; while ($row = mysql_fetch_assoc($val)) { $selectbox.='<option value=\"' . $row['dept_name'] . '\">' . $row['dept_name'] . '</option>'; } $selectbox.='</select>'; echo $selectbox; ?>
how can pass selected value in degree type combobox in place of ???
help me! thanks!
if want values without refreshing whole page, need use ajax. in example, jquery's $.ajax
. (simple same page ajax)
<?php if(isset($_post['get_values'])) { $data = array(); $selected = $_post['value']; $host = "localhost"; $mysql_db = "db"; $mysql_u = "root"; $mysql_p = ""; $con = new mysqli($host, $mysql_u, $mysql_p, $mysql_db); $stmt = $con->prepare('select dept_name dept_mast deg_type = ?'); $stmt->bind_param('s', $selected); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()){ $data[] = $row; } // return value json echo json_encode($result); exit; } ?> <select id="select3" name="deg"> <option value="ug">ug</option> <option value="pg">pg</option> </select> <div id="second_select"></div> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <!-- <script src="jquery.min.js"></script> --> <script type="text/javascript"> $(document).ready(function(){ $('#select3').on('change', function(){ // when user selects value var selected = $(this).val(); // value // request server $.ajax({ url: document.url, // same page process type: 'post', data: {get_values: true, value: selected}, datatype: 'json', success: function(response) { // response of server, , put inside new select box $('#second_select').html('<select name="userst"></select>'); var options = ''; $.each(response, function(index, element){ options += '<option value="'+element.dept_name+'">'+element.dept_name+'</option>'; }); $('select[name="userst"]').html(options); } }); }); }); </script>
sidenote: it's not discouraged, avoid! using mysql_* functions, use newer , improved version
myslqi
or pdo
Comments
Post a Comment