PHP Codeigniter:下拉菜单上的 set_value

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11876604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:20:21  来源:igfitidea点击:

PHP Codeigniter: set_value on dropdown menu

phpcodeigniter

提问by Jess McKenzie

I have the following view but how do I apply set_valueto it without it defaulting to Please select?

我有以下视图,但如何在set_value不默认为请选择的情况下应用它?

<label for="add_fields_type">Type: </label>
<select name="add_fields_type" id="add_fields_type">
    <option value="">Please Select</option>
    <option value="<?php echo set_value('input'); ?>">Input</option>
    <option value="<?php echo set_value('textarea'); ?>">Text Area</option>
    <option value="<?php echo set_value('radiobutton'); ?>">Radio Button</option>
    <option value="<?php echo set_value('checkbox'); ?>">Check Box</option>
</select>

Update:

更新:

View:

看法:

<label for="add_fields_placeholder">Placeholder: </label>
    <select name="add_fields_placeholder" id="add_fields_placeholder">
        <option value="">Please Select</option>
        <option value="<?php echo set_value('yes'<?php echo set_select('add_fields_placeholder','yes', ( !empty($placeholderType) && $placeholderType == "yes" ? TRUE : FALSE ));?>">Yes</option>
        <option value="<?php echo set_value('no' <?php echo set_select('add_fields_placeholder','no', ( !empty($placeholderType) && $placeholderType == "no" ? TRUE : FALSE )); ?>">No</option>
    </select>

    <label for="add_fields_placeholderValue">Placeholder Text: </label>
    <input type="text" name="add_fields_placeholderValue" id="add_fields_placeholderValue" value="<?php echo set_value('add_fields_placeholderValue'); ?>">

    <label for="add_fields_type">Type: </label>
<select name="add_fields_type" id="add_fields_type">
    <option value="">Please Select</option>
    <option value="input" <?php echo set_select('add_fields_type','input', ( !empty($fieldType) && $fieldType == "input" ? TRUE : FALSE )); ?>>Input</option>
    <option value="textarea" <?php echo set_select('add_fields_type','textarea', ( !empty($fieldType) && $fieldType == "textarea" ? TRUE : FALSE )); ?>>Text Area</option>
    <option value="radiobutton" <?php echo set_select('add_fields_type','radiobutton', ( !empty($fieldType) && $fieldType == "radiobutton" ? TRUE : FALSE )); ?>>Radio Button</option>
    <option value="checkbox" <?php echo set_select('add_fields_type','checkbox', ( !empty($data) && $data == "checkbox" ? TRUE : FALSE )); ?>>Check Box</option>
</select>

Controller:

控制器:

$data['fieldType'] = $this->input->get('add_fields_type');
$data['placeholderType'] = $this->input->get('add_fields_placeholder');

Line 16:

第 16 行:

<option value="<?php echo set_value('yes'<?php echo set_select('add_fields_placeholder','yes', ( !empty($placeholderType) && $placeholderType == "yes" ? TRUE : FALSE ));?>">Yes</option>

回答by M. Ahmad Zafar

This should help:

这应该有帮助:

Controller (test.php)

控制器(test.php)

<?php
    class Setup extends CI_Controller {

        function index() {
            //for the set_select() function
            $this->load->library('form_validation');

            //for base_url() function
            $this->load->helper('url');

            $list['data'] = $this->input->get('add_fields_type');

            $this->load->view('test_view.php', $list);
        }
?>

View (test_view.php)

查看(test_view.php)

<!DOCTYPE HTML>
<html>
<body>
<form action="<?php echo base_url(); ?>test">
    <label for="add_fields_type">Type: </label>
    <select name="add_fields_type" id="add_fields_type">
        <option value="">Please Select</option>
        <option value="input" <?php echo set_select('add_fields_type','input', ( !empty($data) && $data == "input" ? TRUE : FALSE )); ?>>Input</option>
        <option value="textarea" <?php echo set_select('add_fields_type','textarea', ( !empty($data) && $data == "textarea" ? TRUE : FALSE )); ?>>Text Area</option>
        <option value="radiobutton" <?php echo set_select('add_fields_type','radiobutton', ( !empty($data) && $data == "radiobutton" ? TRUE : FALSE )); ?>>Radio Button</option>
        <option value="checkbox" <?php echo set_select('add_fields_type','checkbox', ( !empty($data) && $data == "checkbox" ? TRUE : FALSE )); ?>>Check Box</option>
    </select>
    <input type="submit" />
</form>
</body>
</html>

Note:The third parameter of the set_select()determines whether it should be selected or not

注意:的第三个参数set_select()决定是否应该选择

回答by Tony Ciccarone

Sort of what Abid said, you'd simply use set_value() after your form validation.

就像 Abid 所说的那样,您只需在表单验证后使用 set_value() 即可。

form_dropdown('add_fields_type', $field_types, set_value('field_type'));

回答by WatsMyName

Try this:

尝试这个:

<option value="<?php echo set_value('textarea'); ?>" <?php echo (set_value('textarea')=='TEXTAREA')?" selected=' selected'":""?>>Text Area</option>

Above TEXTAREA is the value you are expected to come on the option

高于 TEXTAREA 是您希望在该选项上获得的值

回答by Abid Hussain

Try this :-

尝试这个 :-

See url:-- http://codeigniter.com/user_guide/helpers/form_helper.html

见网址:-- http://codeigniter.com/user_guide/helpers/form_helper.html

$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

$shirts_on_sale = array('small', 'large');

echo form_dropdown('shirts', $options, 'large');

// Would produce:

// 会产生:

<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>

echo form_dropdown('shirts', $options, $shirts_on_sale);

// Would produce:

// 会产生:

<select name="shirts" multiple="multiple">
<option value="small" selected="selected">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>

回答by NULL pointer

To use set_select you need to have a validation rule for the field in question. See my answer to a similar question here: CodeIgniter select_value in form_dropdown

要使用 set_select,您需要为相关字段设置验证规则。在此处查看我对类似问题的回答: CodeIgniter select_value in form_dropdown

回答by Sanoj Dushmantha

it is much easier to use javascript(tested)

使用 javascript 更容易(已测试)

<select name="add_fields_type" id="add_fields_type">
 <option value="">Please Select</option>
 <option value="Input">Input</option>
 <option value="Text Area">Text Area</option>
 <option value="Radio Button">Radio Button</option>
 <option value="Check Box">Check Box</option>
</select>


 <script>
    document.getElementById('select_status').value="<?php echo set_value('status'); ?>";
 </script>