php - Show/hide select values based on previous select choice -
i have 2 select's inside form. second select has 2000 lines in total coming out of mysql table. 1 of column mysql has 1 of values used first select. want able filter on value when selected first select, shows these articles.
code now:
<div class="rmaform"> <select name="discipline" class="discipline"> <option value=" " selected></option> <option value="access">access</option> <option value="inbraak">inbraak</option> <option value="brand">brand</option> <option value="cctv">cctv</option> <option value="airphone">airphone</option> <option value="perimeter">perimeter</option> </select> </div> <div class="rmaform"> <select name="article" class="input-article"> <?php $articleselect = $dbh->prepare('select * articles'); $articleselect->execute(); while($articlerow = $articleselect->fetch(pdo::fetch_assoc)){ ?> <option value="<?php echo $articlerow['a_code'];?>"><?php echo $articlerow['a_code'];?> <?php echo $articlerow['a_omschr_nl'];?></option> <?php } ?> </select>
i think have use javascript how combine php , javascript? , best way make filter work?
jquery change event , ajax
$(document).ready(function(e) { $('select.discipline').change(function(e) { // when select changed var sel_value=$(this).val(); // chosen value $.ajax( { type: "post", url: "ajax.php", // new php page option value, process , return possible options second select data: {selected_option: sel_value}, // send slected option php page datatype:"html", success: function(data) { $('select.input-article').append(data); // append possible values second select } }); }); });
in ajax.php
<?php if(isset($_post['selected_option'])) $selected_option=filter_input(input_post, "selected_option", filter_sanitize_string); else exit(); // no value sent $query="select * articles discipline='$selected_option'"; // example. build query per logic // process query $options=""; while($query->fetch()) // simplicity. proceed pdo { $options.="<option value='option_value'>text option</option>"; // option_value value option , text option text displayed particular option } echo $options; ?>
note: can use json instead of html simplicity. read here, how to.
Comments
Post a Comment