0

I have problem with my codes and I'm still new to php. Please help me :)

echo "<td><select>
    <option value='1'<?php if($row['Staf_Kamp'] == '1') { ?> selected='selected'<?php } ?>>1</option>;

    <option value='2'<?php if($row['Staf_Kamp'] == '2') { ?> selected='selected'<?php } ?>>2</option>;

    <option value='3'<?php if($row['Staf_Kamp'] == '3') { ?> selected='selected'<?php } ?>>3</option>;

</select></td>";

I am expecting the dropdown list that I have selected before edit will be selected in the edit page. But it is not working.

0

3 Answers 3

2

When echoing, you are already in a php context, so there is no need to use <?php ?> tags again. You can just concatenate the variables in your string.

echo "<td><select>

    <option value='1'" . ($row['Staf_Kamp'] == '1' ? ' selected="selected"' : '') . ">1</option>;

    <option value='2'" . ($row['Staf_Kamp'] == '2' ? ' selected="selected"' : '') . ">2</option>;

    <option value='3'" . ($row['Staf_Kamp'] == '3' ? ' selected="selected"' : '') . ">3</option>;

</select></td>";
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

<td><select>
    <option value='1'<?php if($row['Staf_Kamp'] == '1') { echo ' selected'; } ?>>1</option>
    <option value='2'<?php if($row['Staf_Kamp'] == '2') { echo ' selected'; } ?>>2</option>
    <option value='3'<?php if($row['Staf_Kamp'] == '3') { echo ' selected'; } ?>>3</option>
</select></td>

You only need ; when working inside the <?php ?> tags so strip those out. Also save the opening and closing by just echoing out the selected value. Also the correct syntax is selected and not selected='selected'

Comments

0

you can not echo a string and put php code into the string

correct code:

<td>
<select>
    <option value='1'<?php if($row['Staf_Kamp'] == '1') { ?> selected='selected'<?php } ?>>1</option>
    <option value='2'<?php if($row['Staf_Kamp'] == '2') { ?> selected='selected'<?php } ?>>2</option>
    <option value='3'<?php if($row['Staf_Kamp'] == '3') { ?> selected='selected'<?php } ?>>3</option>
</select>
</td>

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.