简体   繁体   English

构建标记后,选择PHP选项

[英]Select Option with PHP when the markup is built

NEW TO PHP (selection and form) Here's my problem: PHP的新功能(选择和表单)这是我的问题:

Let's say that I have a select element with some option list and, based on the page that I'm in, I want to automatic select my option: 假设我有一个带有一些选项列表的select元素,并且我要根据我所在的页面来自动选择我的选项:

<select name="program_">
    <option value="myvalue">my value</option>
    <option value="myvalue2">my other value</option>
    <option value="myvalue3">my last value</option>
</select>

I created a script based on a plugin that I'm using for custom post and field, and it works in this way (PHP): 我基于用于自定义帖子和字段的插件创建了一个脚本,并且该脚本以这种方式工作(PHP):

            <?php if (CFS()->get( 'program_code' )): ?>
                <option value="<?php echo CFS()->get( 'program_code' ) ?>" selected="selected"><?php echo CFS()->get( 'program_code' ) ?></option>"
            <?php else: ?>
                <option value="" selected="selected">Program of interest *</option>"
            <?php endif; ?>

And I added right after my tag. 我在标签之后添加了。

Now, it works fine but the problem is that it duplicate my field. 现在,它工作正常,但问题是它重复了我的字段。 Essentially the final mark up will be, based on the dinamic sidebar that it will create something like that: 本质上,最终标记将基于动态侧边栏,它将创建如下所示的内容:

<select name="program_">
        <option value="myvalue" selected="selected">my value</option>
        <option value="myvalue">my value</option>
        <option value="myvalue2">my other value</option>
        <option value="myvalue3">my last value</option>
</select>

Like I said the script will duplicate the value, instead I would love to create only the value that I need to add to my selection. 就像我说的那样,脚本将复制值,相反,我只想创建需要添加到选择中的值。

Is there something that I'm doing wrong? 我做错什么了吗?

PLUS, it is gonna add the element at the beginning of the list, and I'd like to select my option in the exact position where the option selected is found. 另外,它将元素添加到列表的开头,我想在找到所选选项的确切位置选择我的选项。

The meaning of your if/else code is to test if that condition EXISTS. 您的if / else代码的含义是测试该条件是否存在。

If you want to code for several conditions, you need the FOR loop. 如果要针对多个条件进行编码,则需要FOR循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM