Welcome Guest, Not a member yet? Register   Sign In
Drop Down List Unable to Pass Value
#1

[eluser]deliriumfolk[/eluser]
How come when I pass my selected option, it always being seen as 0.
Hence does not post the correct value according to what I had selected in the drop down list.
What might be amiss that does not allow my controller to post in the correct value?

Code:
Model
public function set_user(){
                
        $data = array(
        'username' => $this->input->post('username'),
        'password' => $this->input->post('password'),
            'name' => $this->input->post('name'),
            'email' => $this->input->post('email'),
            'title' => $this->input->post('title'),
            'groupId' => $this->input->post('groupId')
        );
        
        return $this->db->insert('user', $data);
    }
Code:
Controller

public function adduser() {
        
        //$this->load->helper('form');
        $this->load->library('form_validation');
        //Get the user group data so it will be available for drop down list selection
        $data['usergroup'] = $this->usergroup_model->get_usergroup();
        
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
     $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
        $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
        $this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
        $this->form_validation->set_rules('usergroup', 'User Group', 'required');
        
        $data['title'] = 'CMS Add User';
        
        if($this->form_validation->run() == FALSE)
        {
            //Field validation failed hence user redirected back to add user form
            $this->load->helper(array('form', 'url'));
            $this->load->view('templates/cms/header', $data);
            $this->load->view('cms/adduser', $data);
            $this->load->view('templates/cms/footer');
        }
        else {
            $this->user_model->set_user();
            redirect('cms/listuser', 'refresh');
        }    
    }

Code:
View


<?php echo validation_errors(); ?>
<?php echo form_open('cms/adduser'); ?>

    <?php echo form_label('Username: ', 'username'); ?>
    <input size="24" id="username" name="username" value="<?php echo set_value('username'); ?>"/>
    <br/>
    &lt;?php echo form_label('Password: ', 'password'); ?&gt;
    &lt;input type="password" size="24" id="passowrd" name="password"/&gt;
    <br/>
    &lt;?php echo form_label('Name: ', 'name'); ?&gt;
    &lt;input type="text" size="24" id="name" name="name" value="&lt;?php echo set_value('name'); ?&gt;"/&gt;
    <br/>
    &lt;?php echo form_label('Email: ', 'email'); ?&gt;
    &lt;input type="text" size="24" id="email" name="email" value="&lt;?php echo set_value('email'); ?&gt;"/&gt;
    <br/>
    &lt;?php echo form_label('Job Title: ', 'title'); ?&gt;
    &lt;input type="text" size="24" id="title" name="title" value="&lt;?php echo set_value('title'); ?&gt;"/&gt;
    <br/>
    User Group: <select name="usergroup">
        <option value="" >--Select--</option>
        &lt;?php foreach ($usergroup as $usergroup_item) { ?&gt;
        <option value="&lt;?php echo $usergroup_item['groupId'] ?&gt;">&lt;?php echo $usergroup_item['name'] ?&gt;</option>
        &lt;?php } ?&gt;
    </select>
    <br/>
    &lt;?php echo form_submit('submit', 'Add User'); ?&gt;
&lt;/form&gt;
#2

[eluser]InsiteFX[/eluser]
Why not use the form_helpers dropdown?
#3

[eluser]deliriumfolk[/eluser]
Will changing my view to use form_dropdown solve the issue i am having?
#4

[eluser]dnc[/eluser]
What does the view source look like at lines:

Code:
User Group: <select name="usergroup">
        <option value="" >--Select--</option>
        &lt;?php foreach ($usergroup as $usergroup_item) { ?&gt;
        <option value="&lt;?php echo $usergroup_item['groupId'] ?&gt;">&lt;?php echo $usergroup_item['name'] ?&gt;</option>
        &lt;?php } ?&gt;
    </select>

Are all the variables being passed?
#5

[eluser]dnc[/eluser]
Also you can do a var dump instead of the return in your model to be sure the drop down variable value is being passed.

Code:
Model
public function set_user(){
                
        $data = array(
        'username' => $this->input->post('username'),
        'password' => $this->input->post('password'),
            'name' => $this->input->post('name'),
            'email' => $this->input->post('email'),
            'title' => $this->input->post('title'),
            'groupId' => $this->input->post('groupId')
        );


        var_dump($data);
        exit();
        
        //return $this->db->insert('user', $data);
    }
#6

[eluser]deliriumfolk[/eluser]
This is what the drop down will display.

Code:
<select name="usergroup">
<option value="">--Select--</option>
<option value="1">Administrator</option>
<option value="2">Editor</option>
<option value="3">Writer</option>
<option value="4">Translator</option>
<option value="5">Designer</option>
</select>

This is what it is being pass through. Why is it a Boolean?

Code:
array (size=6)
  'username' => string 'Writer' (length=6)
  'password' => string 'password' (length=8)
  'name' => string 'Writer' (length=6)
  'email' => string '[email protected]' (length=17)
  'title' => string 'Tech Writer' (length=11)
  'groupId' => boolean false
#7

[eluser]Aken[/eluser]
In your view, your select menu has a different name.
#8

[eluser]deliriumfolk[/eluser]
Hey, thanks guys for all your feedback. I got it working.
Aken had pointed it out that in my view the select menu has a different name.

What change in here is changing the name of the select to groupId.
Code:
View

User Group: <select name="groupId">
    <option value="" >--Select--</option>
    &lt;?php foreach ($usergroup as $usergroup_item) { ?&gt;
    <option value="&lt;?php echo $usergroup_item['groupId'] ?&gt;">&lt;?php echo $usergroup_item['name'] ?&gt;</option>
    &lt;?php } ?&gt;
</select>

And in here I changed the form_validation to groupId as well.
Code:
Controller

$this->form_validation->set_rules('groupId', 'User Group', 'required');

I believe the following changes allow the correct value from the drop down to pass to the model which is waiting to receive attribute from "groupId".




Theme © iAndrew 2016 - Forum software by © MyBB