0

I have a dropdown list with customer names from database. I want these values to be a link with my 'href' attribute. How can I do it?

 <select class="feedback-input" id="customer_selecter" name="customerName"> 
            <option >Select customer</option>
            <?php foreach ($customers as $row): ?>
                <a href="<?php echo base_url() . "index.php/edit/show_customer_id/" . $row->customerID; ?>">
            <?php echo '<option value="'.$row->customerID.'">'.$row->customerName.'</option>'; ?></a>
            <?php endforeach; ?>
        </select>  
Ksenia
  • 17
  • 1
  • 10
  • 3
    Possible duplicate of [using href links inside – roberto06 Aug 09 '17 at 14:51

1 Answers1

1

try this:

<select class="feedback-input" id="customer_selecter" name="customerName"> 
    <option >Select customer</option>
    <?php foreach ($customers as $row): ?>
        <a href="<?php echo base_url() . 'index.php/edit/show_customer_id/' . $row->customerID; ?>">
            <option value="<?php echo $row->customerID; ?>"><?php echo $row->customerName; ?></option>
        </a>
    <?php endforeach; ?>
 </select> 

but this is not a valid html, because you're using a <a> tag inside a <select> tag, try doing this with javascript instead like this :

 <select class="feedback-input" id="customer_selecter" name="customerName" onchange="location = this.value;"> 
    <option >Select customer</option>
    <?php foreach ($customers as $row): ?>
        <option value="<?php echo base_url() . 'index.php/edit/show_customer_id/' . $row->customerID; ?>"><?php echo $row->customerName; ?></option>
    <?php endforeach; ?>
 </select> 
yoeunes
  • 2,927
  • 2
  • 15
  • 26
  • @Ksenia try the seconds method – yoeunes Aug 09 '17 at 14:59
  • @Ksenia This is the same method in the answer that this is marked as a duplicate of. If you couldn't get that answer to work, then you were doing something wrong. – Patrick Q Aug 09 '17 at 15:03
  • @PatrickQ you're right this is the same method, i'm just applying it on his own code, i just fix his echo concatenation and order with the `option` tag – yoeunes Aug 09 '17 at 15:10