-1

I'm trying to auto fill an input form based on the selection of a selectbox. the auto fill text comes from my database

This is my code:

<select style="width: 110px;" class="form-control" name="id_periodik" id="id_periodik" required oninvalid="this.setCustomValidity('Masukkan periodik sparepart!')"
oninput="this.setCustomValidity('')">
<?php foreach ($periodik_options as $option) {?>
<option value="<?php echo $option['id_periodik']; ?>">
<?php echo $option['nama_periodik']; ?>
</option>
<?php }?>
</select>
</td>

text auto fill based on selection:

<td>
<input type="text" class="form-control" id="jumlah_periodik" name="jumlah_periodik[]" readonly>
</td>

script:

<script>
$(document).ready(function() {
$('#id_periodik').on('change', function() {
var selectedValue = $(this).val();
$('#jumlah_periodik').val(selectedValue);
});
});
</script>

i've try that but nothing happens in my view. Any help you can give I appreciate it enter image description here

Fariz
  • 3
  • 1
  • Always check [the console of your browser](https://balsamiq.com/support/faqs/browserconsole/) for error messages. Then [try the answer here](https://stackoverflow.com/questions/10659097/jquery-get-selected-option-from-dropdown). – KIKO Software Jul 13 '23 at 09:48
  • Is there a race condition going on between the inline `oninput="this.setCustomValidity('')"` and the external jQuery event handler or is the former function preventing the jQuery somehow? – Professor Abronsius Jul 13 '23 at 10:24

1 Answers1

-1
$(document).ready(function() {
    $('#id_periodik').on('change', function() {
        // here is the change
        var selectedValue = $('#id_periodik').find(":selected").val();

        $.ajax({
                type : "Get",
                url: 'your_url',
                data: {id_periodik: selectedValue},
                success:function(result){
                    $('#jumlah_periodik').val(result);
                }
            });
    });
});
1. Get selected id_periodik 2. Then send ajax request to get data from database 3. Then set data to jumlah_periodik Hope this work
kalam
  • 34
  • 4
  • Please add a clear explanation as to why this is the answer to the question. – KIKO Software Jul 13 '23 at 11:09
  • Please check .does it work or not – kalam Jul 13 '23 at 12:01
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 17 '23 at 09:54