0

I have a dropdown list which filled with the data fetched from db. I need to assign the selected index value of this dropdown list as the ID of a button. Is it really possible?

my code

<select name="customer" onchange="getCustomer()"  id="customer" class="form-control" style="width: 180px !IMPORTANT;">
 <option value="">--Select Customer--</option>
                                 <?php
                                 $sql = mysqli_query($db,"SELECT * FROM customer ");
                                 while($row= mysqli_fetch_array($sql))
                                 {
                                 ?>
                                 <option value="<?php echo $row['custid'];?>"><?php echo $row['customername'];?></option>
                                 <?php
                               }
                                 ?>

 </select>
                                    </div>
                                    <button name="custbt" id="1" class="label-text col-form-label userinfo"><a href="#vCenteredModal" data-toggle="modal">Show customer details</a></button> 

  <script>
          function getCustomer()
          {
            var e = document.getElementById("customer");
            var cust = e.options[e.selectedIndex].value;
          //  alert(cust);custbt
            //document.getElementByName("custbt").id=cust;
            document.getElementByName(custbt).id=cust;

          }
    </script>
3
  • it is possible, what doesn't work for you? Commented Jan 28, 2019 at 11:06
  • you should set data-id instead of id. Commented Jan 28, 2019 at 11:06
  • custbt is undefined, therefor document.getElementByName(custbt).id=cust; will not work. Commented Jan 28, 2019 at 11:08

3 Answers 3

2

Might be this will solve your problem. I have added a class on btn call CustomerModalBttn .

Try this

<select name="customer" id="customer" class="form-control" style="width: 180px !IMPORTANT;">
<option value="">--Select Customer--</option>
  <?php
  $sql = mysqli_query($db,"SELECT * FROM customer ");
  while($row= mysqli_fetch_array($sql)){
  ?>
     <option value="<?php echo $row['custid'];?>"><?php echo $row['customername'];?></option>
  <?php
  }
  ?>
</select>

<button name="custbt" id="1" class="label-text col-form-label userinfo CustomerModalBttn">
  <a href="#vCenteredModal" data-toggle="modal">Show customer details</a>
</button> 

<script>
  $(document).ready(function(){
     $('#customer').on('change',function(){
        $('.CustomerModalBttn').attr('id',$(this).val());
     });
  });
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

IDs must begin with a letter.

<button name="custbt" id="a1" class="label-text col-form-label userinfo"><a href="#vCenteredModal" data-toggle="modal">Show customer details</a></button>

var customer = document.getElementById("customer");
    var a1 = document.getElementById("a1");

    customer.addEventListener("change",function(){
        a1.removeAttribute("id");
        a1.setAttribute("id",customer.value);
    });

1 Comment

And maybe change <option value="<?php echo $row['custid'];?>"> to <option value="a<?php echo $row['custid'];?>">
0

Just add some condition if ($row['custid'] == $_GET['custid']) echo 'selected', if you pass the customer id from url, and then get it with $_GET param. ...

while($row= mysqli_fetch_array($sql))
{ ?>
   <option <?php if ($row['custid'] == $_GET['custid']) echo 'selected'; ?> value="<?php echo $row['custid'];?>">
<?php echo $row['customername'];?>
</option>
<?php } ?>
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.