3

I tried below code but it is not set the selected value :

<?php
   $form_field1 .= "<select name='typeofleave'> 
                   <option value='LWP'<?=$typeofleave == 'LWP' ? ' selected='selected'' : '';?> >LWP</option>
                    <option value='SL'<?=$typeofleave == 'SL' ? ' selected='selected'' : '';?> >SL</option>
                    <option value='PL'<?=$typeofleave == 'PL' ? ' selected='selected'' : '';?> >PL</option>
                </select>";
?>

Please help to solve it..

0

4 Answers 4

4

Let me show you very neat method of doing this.

1) Take an array of options

2) Loop over it to get select <option>s.

3) Check if current is selected in loop.

Corrected code:

<?php
$form_field1 .= '<select name="typeofleave">';
$options = array();
$options['LWP'] = 'LWP';
$options['SL'] = 'SL';
$options['PL'] = 'PL';
if (! empty($options)) {
 foreach ($options as $option) {
  $selected = ($typeofleave == $option) ? 'selected="selected"' : '';
  $form_field1 .= '<option value="'.$option.'" ' . $selected . '>'.$option.'</option>';
 }
}
$form_field1 .= '</select>';
?>
Sign up to request clarification or add additional context in comments.

Comments

3

Try below code.

$form_field1 = '';
$typeofleave = 'SL';

echo $form_field1 .= "<select name='typeofleave'>
                 <option value='LWP'".($typeofleave == 'LWP' ? ' selected' : '' )." >LWP</option>
                 <option value='SL'".($typeofleave == 'SL' ? ' selected' : '' ).">SL</option>
                 <option value='PL'".($typeofleave == 'PL' ? ' selected' : '' ).">PL</option>
            </select>";

Comments

2

Your quotes seems a bit off:

' selected='selected''

Should be

' selected="selected"'

3 Comments

Not taking into account that there are syntax errors, opening PHP tags inside PHP. But you're not wrong either!
I was about to add that in (and give you the cred for the opening tags ;-) ) when I saw that he accepted an answer.
Might as well edit it - you never know who'll come looking in the future! ;) @MagnusEriksson
2

It should be a single selected keywork - not selected=xxx. And only one option should have it.

<?php
$form_field1 .= "<select name='typeofleave'>"
                . "<option value='LWP'" . ($typeofleave == 'LWP' ? ' selected' : '') . ">LWP</option>"
                . "<option value='SL'" . ($typeofleave == 'SL' ? ' selected' : '') . ">SL</option>"
                . "<option value='PL'" . ($typeofleave == 'PL' ? ' selected' : '') . " >PL</option>"
            . "</select>";
?>

5 Comments

Actually, selected="selected" is valid and should work... and he is checking for it on each, and only setting it on a match.
Also, if you look at the highlights, there are syntax errors here. Opening PHP tags inside PHP.
You mean the <?= one - no this is a valid syntax if you allow the short opening tag. Both <? and <?php are valid syntax as long you enable it in your php.ini
@cyrille - You can't open a php block inside an already opened php block. Short tags or no short tags.
oh yes, did not saw that one :S I edited the code now to reflect your comment - my mistake

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.