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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP