PHP Codeigniter: set_value on dropdown menu

前端 未结 6 2223
温柔的废话
温柔的废话 2021-01-05 04:11

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

相关标签:
6条回答
  • 2021-01-05 04:22

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

    form_dropdown('add_fields_type', $field_types, set_value('field_type'));
    
    0 讨论(0)
  • 2021-01-05 04:22

    it is much easier to use javascript(tested)

    <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>
    
    0 讨论(0)
  • 2021-01-05 04:27

    Try this :-

    See url:-- 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>
    
    0 讨论(0)
  • 2021-01-05 04:31

    This should help:

    Controller (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)

    <!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

    0 讨论(0)
  • 2021-01-05 04:33

    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

    0 讨论(0)
  • 2021-01-05 04:45

    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

    0 讨论(0)
提交回复
热议问题