PHP Codeigniter:下拉菜单中的set_value

时间:2012-08-09 03:59:07

标签: php codeigniter

我有以下观点,但如果不将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>

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

更新

查看:

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

控制器:

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

第16行:

{{1}}

6 个答案:

答案 0 :(得分:7)

这应该有所帮助:

控制器(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);
        }
?>

查看(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>

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

答案 1 :(得分:5)

Abid所说的内容,你只需在表单验证后使用set_value()。

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

答案 2 :(得分:3)

试试这个:

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

以上TEXTAREA是您希望选项

的价值

答案 3 :(得分:0)

试试这个: -

请参阅网址: - 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');

//会产生:

<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);

//会产生:

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

使用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>