0

I want to move the name attribute, to be a hidden value (not id), but ... stored in the database that is still id

<div class="form-group col-md-4">
    <select name="id_cs" id="pakyu" class="form-control select2" style="width: 100%;" onchange="ganti();">
        <option value="0">-PILIH-</option>
        <?php foreach($datacs->result() as $row):?>
        <option value="<?php echo $row->id_cs;?>"><?php echo $row->nama_cs;?></option>
        <?php endforeach;?>

        <input type="hidden" id="csnya" name="nama_cs">
    </select>
</div>

jquery

function ganti() {
    var reg = $('#pakyu option:selected').val();
    $('#csnya').val(reg);
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Tito
  • 17
  • 6
  • 1
    what is `reg` here? I do not see it in your PHP/HTML code. – Himanshu Upadhyay Aug 21 '19 at 08:18
  • 2
    You should move your hidden input to be before or after the select, not inside of it. I also don't really understand what you're trying to do and what the issue is? Please read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – M. Eriksson Aug 21 '19 at 08:18
  • Possible duplicate [jQuery get the name of a select option](https://stackoverflow.com/questions/18697843/jquery-get-the-name-of-a-select-option) – M. Eriksson Aug 21 '19 at 08:22

1 Answers1

0

Do some changes like this:

<div class="form-group col-md-4">
    <select name="id_cs" id="pakyu" class="form-control select2" style="width: 100%;" onchange="ganti();">
        <option value="0">-PILIH-</option>
        <?php foreach($datacs->result() as $row):?>
        <option value="<?php echo $row->id_cs;?>"><?php echo $row->nama_cs;?></option>
        <?php endforeach;?>
    </select>
    <input type="hidden" id="csnya" name="nama_cs" value=""> <!--moved this out from select tag -->
</div>

jQuery:

function ganti() {
    var reg = $('#pakyu option:selected').text();  // Use text function
    $('#csnya').val(reg);
}
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33