Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Codeigniter: set_value on dropdown menu

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

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

<option value="<?php echo set_value('yes'<?php echo set_select('add_fields_placeholder','yes', ( !empty($placeholderType) && $placeholderType == "yes" ? TRUE : FALSE ));?>">Yes</option>
like image 679
Jess McKenzie Avatar asked Aug 09 '12 03:08

Jess McKenzie


1 Answers

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

like image 85
M. Ahmad Zafar Avatar answered Sep 18 '22 14:09

M. Ahmad Zafar