php - How to show dropdowns with selected options if form is submitted and have validation errors -
i have form radio buttons first. when choose radio button, shown different dropdowns different radio buttons. dependent dropdowns. first hide them all. it's working, when click submit button , there fields or options have not filled, these dropdowns hide , have select them again. want make if click submit button , there validation errors, show these dropdowns radio, have chosen, , choices selected. here's code:
$(document).ready(function(){ $(":radio").click(function(){ $('#region').val('choose region'); $('#school').val('choose region'); $('#teacher').val('choose school'); $('#class').val('choose class'); $('#class_divisions').val('choose division'); }); }); function showhide(self, show){ $(".all_teachers_show, .toggle, .school, .teacher_school, .teacher, .class, .teacher_class").hide(); if (show) $('.toggle').show(); else $('.toggle').hide(); $(":radio").prop('checked',false); $(self).prop('checked',true); } function show(yes, no){ if (no) $('.school').show(); else $('.school').hide(); $("region").prop('checked',false); $(yes).prop('checked',true); } function school_show(yes, no){ if(document.getelementbyid("radio1").checked===true){ document.getelementbyid('class_label').innerhtml = 'class:*'; if (no) $('.teacher').show(); else $('.teacher').hide(); $("school").prop('checked',false); $(yes).prop('checked',true); } if(document.getelementbyid("radio2").checked===true){ document.getelementbyid('class_label').innerhtml = 'class teacher:*'; if (no) $('.class').show(); else $('.class').hide(); $("school").prop('checked',false); $(yes).prop('checked',true); } } function class_show(yes, no){ if (no) $('.class').show(); else $('.class').hide(); $("teacher").prop('checked',false); $(yes).prop('checked',true); } function teachers_show(yes, no){ $(".toggle, .all_teachers_show, .school, .teacher_school, .teacher, .class, .teacher_class").hide(); if (no) $('.all_teachers_show').show(); else $('.all_teachers_show').hide(); $(":radio").prop('checked',false); $(yes).prop('checked',true); } </script> <script> $(document).ready(function() { $('#region').change(function() { var url = "<?= base_url() ?>index.php/home/get_schools"; var postdata = {region: $('#region').val()}; $.post(url, postdata, function(result) { var $school_sel = $('#school'); $school_sel.empty(); $school_sel.append("<option>choose region</option>"); var schools_obj = json.parse(result); $.each(schools_obj, function(key, val) { var option = '<option value="' + val.school_id + '">' + val.school_name + '</option>'; $school_sel.append(option); }); }); }); }); </script> <script> $(document).ready(function() { $('#school').change(function() { var url = "<?= base_url() ?>index.php/home/get_teachers"; var postdata = {school: $('#school').val()}; $.post(url, postdata, function(result) { var $teacher_sel = $('#teacher'); $teacher_sel.empty(); $teacher_sel.append("<option>choose school</option>"); var teachers_obj = json.parse(result); $.each(teachers_obj, function(key, val) { var option = '<option value="' + val.user_id + '">' + val.username + '</option>'; $teacher_sel.append(option); }); }); }); });
<?php echo validation_errors(); echo "<div class='container'>"; echo form_open(); echo "<table border = '0' >"; echo "<tr><td><label> username:* </label></td><td>"; $data=array( 'name' => 'username', 'class' => form_error('username') ? 'error' : '', 'value' => set_value('username') ); echo form_input($data); echo "</td></tr>"; echo "<tr><td><label> password:* </label></td><td>"; $data=array( 'name' => 'password', 'class' => form_error('password') ? 'error' : '' ); echo form_password($data); echo "<tr><td><label> choose role:* </label> </td><td>"; <input type="radio" name="role_id" onclick='showhide(this, true)' id="radio1" <?php echo set_radio('role_id', '1'); ?> /> <?php echo " student "; $data=array( 'name' => 'role_id', 'value' => set_value('role_id', '2'), 'id' => 'radio2', 'onclick' => 'showhide(this, true)' ); echo form_radio($data); echo " teacher "; $data=array( 'name' => 'role_id', 'value' => set_value('role_id', '5'), 'id' => 'radio5', 'onclick' => 'teachers_show(this, true)' ); echo form_radio($data); echo " coord. "; echo "</td></tr>"; echo "<tr class='toggle' style='display:none;' ><td><label> region:* </label></td><td>"; ?> <select name='region' id='region' onchange='show(this, true)'> <option <?php echo set_select('region'); ?>>choose region</option> <?php foreach ($regions $row) { ?> <option name='region' value= "<?= $row->region ?>"><?= $row->region ?></option> <?php } ?> </select> <?php echo "</td></tr>"; echo "<tr class='school' style='display:none;' ><td><label> school:* </label></td><td>"; ?> <select id="school" name="school[]" onchange='school_show(this, true)'> <option <?php echo set_select('school[]'); ?>>choose region</option> </select> <?php echo "</td></tr>"; echo "<tr class='teacher' style='display:none;' onchange='class_show(this, true)'><td><label> teacher:* </label> </td><td>"; echo '<select id="teacher" name="teacher[]">'; ?> <option <?php echo set_select('teacher'); ?>>choose school</option> </select> </td></tr> <?php echo "<tr class='class' style='display:none;'><td> <label id='class_label'> Клас:* </label> </td><td>"; ?> <select name='class[]' id='class'> <option <?php echo set_select('class[]'); ?>>choose class</option> <?php foreach ($classes $row) { ?> <option value= "<?= $row->id ?>"><?= $row->class_id ?></option> <?php } ?> </select> <select name='class_divisions[]' id='class_divisions'> <option <?php echo set_select('class_divisions[]'); ?>>choose division</option> <?php foreach ($class_divisions $row) { ?> <option value= "<?= $row->id ?>"><?= $row->division ?></option> <?php } ?> </select> </td></tr> <?php echo "<tr class='all_teachers_show' style='display:none;'><td><label> teachers:* </label></td><td>"; ?> <?php foreach ($all_teachers_show $row) { ?> <input type="checkbox" id='all_teachers_show' <?php echo set_checkbox('all_teachers_show[]'); ?> name="all_teachers_show[]" value="<?= $row->user_id ?>"><?= $row->username ?> <?php } echo "</td></tr>"; echo "</div>"; echo "</table><br/>"; $data=array( "name" => 'mysubmit', 'class' => 'btn btn-success ', 'id' => 'reg', 'value' => 'register' ); echo form_submit($data); ?> </form>
Comments
Post a Comment