doushuhuai7247 2016-10-12 13:02
浏览 19
已采纳

选中的回声选项值

Im tryin to fix when i press my search button. That the selected search from my option field remains selected. But at the moment it automaticly picks the first field of the options in my form.

First one is hardcoded and it works.

<option value="HS" <?= ($nickval == 'HS' ? 'selected="selected' : '')?>>Homer Simpsons</option>

But then i wanted to echo out option value from database so its not hardcoded.

<?php
while(db2_fetch_row($queryexe)) {
 echo "<option value='$pin'>$fullname</option>";
}
?>   

And now when i want to add if its selected i tried to solve it like this.

echo "<option value='$pin'($nickval == '$pin' ? 'selected='selected'' : '')>$fullname </option>";

This is how i get my pin

 $pin = db2_result($queryexe, 'P510PIN');

This is how i get my $nickval

 $nickval = $_GET["int"];

Any suggestions what im doin wrong? Sorry if im unclear but i've tried my best

  • 写回答

1条回答 默认 最新

  • doujianqin5172 2016-10-12 13:07
    关注

    Aside from quoting errors indicated in the syntax highlighting...

    You're trying to execute PHP code inside of a string:

    echo "<option value='$pin'($nickval == '$pin' ? 'selected='selected'' : '')>$fullname </option>";
    

    Variable interpolation is one thing, but code inside of a string isn't going to automatically execute. It's just a string being echoed to the page. (Check the page source and see what's actually being emitted to the browser.)

    Separate the strings from the code which builds the strings:

    echo "<option value='$pin' " . ($nickval == $pin ? "selected='selected'" : "") . ">$fullname </option>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制