Theun Arbeider
Theun Arbeider

Reputation: 5409

Set Selected value after php foreach

Currently I fill a select box with the following code:

<select name="klas_jaar" value="{jaar_id}">
    <?php foreach ($jaren as $jaar):?>
        <option value="<?=$jaar->jaar_id?>"><?=$jaar->schooljaar?></option>
    <?php endforeach;?>
</select>

The value set is the same id as a option's value.. however it doesn't put the requested value as selected when going to this page.

What am I doing wrong?

Upvotes: 0

Views: 9968

Answers (3)

Netwons
Netwons

Reputation: 1648

in laravel

<option
        @if(count($jaren)==1 && $key === $jaar->category_id) ) selected
        @endif  value="{{$key}}">{{$value}}
    </option>

Upvotes: 0

Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 2138

    <?php foreach($city as $row){?>

    <option value="<?php echo $row['city_uid'];?>" <?php if($row['city_uid'] == $leaddetails[0]['city']){echo "selected";}?>><?php echo $row['city_name']?></option>

  <?php }?>

Upvotes: 0

stevevls
stevevls

Reputation: 10843

select tags don't have a value attribute. Instead, you have to drop a selected attribute in the appropriate option.

<select name="klas_jaar">
    <?php foreach ($jaren as $jaar):?>
        <option value="<?=$jaar->jaar_id?>"<? if($jaar->jaar_id == $selected_jaar_id) echo " selected"?>><?=$jaar->schooljaar?></option>
    <?php endforeach;?>
</select>

Upvotes: 12

Related Questions