0

I am trying to pass the selected value into a hidden field but I always get the last value of option.

   <div class="form-group">
        <label class="col-sm-3 control-label"><?php echo get_phrase('section');?></label>
        <div class="col-sm-5">
            <select name="section_id" class="form-control selectboxit" style="width:100%;">
            <?php
                foreach($sections as $row):
            ?>
            <option value="<?php echo $row['section_id'];?>"><?php echo $row['name'];?></option>
            <?php endforeach;?>
            </select>
        </div>
    </div>
     <input type="text" name="section_id" value="<?php echo $row['section_id']?>">



<div class="form-group">
    <label class="col-sm-3 control-label"><?php echo get_phrase('subject');?></label>
    <div class="col-sm-5">
        <select name="subject_id" class="form-control selectboxit" style="width:100%;">
        <?php
            $subjects = $this->db->get_where('enroll' , array('section_id' => $section_id))->result_array();
            foreach($subjects as $row):
        ?>
        <option value="<?php echo $row['student_id'];?>"><?php echo $row['name'];?></option>
        <?php endforeach;?>
        </select>
    </div>
</div>

i want to pass the hidden field value to query inside the second dropdown how to do this

George Kagan
  • 5,913
  • 8
  • 46
  • 50
sud
  • 31
  • 7
  • where you are sending this values? – Amruth Dec 26 '16 at 04:20
  • Post details about your requirements. – Md. Abutaleb Dec 26 '16 at 04:21
  • i want to send this value to query within another selectbox down there
    – sud Dec 26 '16 at 04:23
  • @sud send that id to any function onchange event. then you can take that value. http://stackoverflow.com/questions/41260223/get-two-values-from-select-option-at-once/41260621#41260621 – Amruth Dec 26 '16 at 04:27
  • yes but how to send that value to the query inside? – sud Dec 26 '16 at 04:28

1 Answers1

0

You can not directly pass section_id value to a text field, because it will always give last section_id. You should use below jquery to assign selected option value to text field:

$(document).ready(function () {
    $('.selectboxit').on('change', function() {
        $('#section_id').val(this.value);
    })
});
Rizban Ahmad
  • 139
  • 1
  • 13
  • i gave the this to an alert alert("section"+$('#section_id').val(this.value)); it gives me [object Object] any reason? – sud Dec 26 '16 at 04:36
  • try to print this value into your console tab in your browser. and fetch required value: console.log(this.value); – Rizban Ahmad Dec 26 '16 at 04:39
  • ok when it gives like console.log(this.value): it prints the expected values..now is that possible to assign this value to php varaible? – sud Dec 26 '16 at 04:41
  • Since PHP is a Server side language and JavaScript is client side. So directly you can not assign js value to php variable. Either you need to pass this value as query parameter or via Ajax. – Rizban Ahmad Dec 26 '16 at 05:03
  • i tried to give like this an getting neither display anything nor error $(document).ready(function () { $('.selectboxit1').on('change', function() { var val=(this.value); alert("val"+val); $.ajax({ alert( url: 'index.php?admin/get_class_section_by_section/' + val , success: function(response) { //jQuery('#section_subject_selection_holder').html(response); } }); }) }); – sud Dec 26 '16 at 05:18
  • @sud, Use this code and try to fix your issue: `$(document).ready(function () { $('.selectboxit1').on('change', function() { var val=(this.value); $.ajax({ type: "POST", url: 'index.php?admin/get_class_section_by_section/'+val, success: function(response) { $('#section_subject_selection_holder').html(response)‌​; } }); }); });` – Rizban Ahmad Dec 26 '16 at 05:34