duanmo6937 2018-10-29 20:53
浏览 122

在php中满足if条件时,将复选框设置为“已选中”

I want to mark a checkbox as 'checked' automatically when an if condition is fulfilled. Here is an example of the if condition-

if($wp_session['tdwa_verification_checks'] < 2){

}

And the checkbox is-

<input class="input-text a-save" type="checkbox" id="chkboxclicked" name="tdwa-foreign-citizen" value="1">

I am trying with this but its not working.

if($wp_session['tdwa_verification_checks'] < 2){

    echo '<input class="input-text a-save" type="checkbox" id="chkboxclicked" name="tdwa-foreign-citizen" value="1" checked>'; 

}

I would appreciate if anyone can give me a clue. Thanks :)

  • 写回答

2条回答 默认 最新

  • douxian1892 2018-10-29 21:19
    关注

    A couple issues. One is that you're wrapping you're entire checkbox inside an if statement. Another is that it's very strange to check for a boolean value by comparing it to less than 2 as you'll generally compare it to equal 1. Another is that chkboxclicked is a very vague ID to say that least, you should probably change that to something more similar to the name. You should also add the closing slash since checkbox inputs are void elements.

    Now, looking at your code, you're also checking $wp_session['tdwa_verification_checks'] but the input name is tdwa-foreign-citizen, are you sure that the key you're checking in $wp_session is correct?

    Lastly, WordPress has a neat function called checked() that will compare and check values for you if applicable. This is how you should probably be using it in your markup:

    <input class="input-text a-save" type="checkbox" id="tdwa-foreign-citizen" name="tdwa-foreign-citizen" value="1" <?php checked( $wp_session['tdwa_verification_checks'], 1 ); ?> />
    
    评论

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失