Welcome Guest, Not a member yet? Register   Sign In
Help Needed with Dropdown Selection
#1

[eluser]TerryT[/eluser]
Finally wasted enough hours beating this around. Any help would be appreciated. I have an input form dropdown. I want the user to select a project off the dropdown before the data gets posted to the database so the items can be posted to the right project.

View Code:
Code:
<div class='dropdown'>
  <label>Projects</label>
  &lt;?php echo form_dropdown('proj_select', $proj_menu); ?&gt;
</div>

Controller Code:
Code:
$selection = 0;
$selection = $this->input->post('proj_select');
if($selection != 0)  //Make sure a project is selected.

It doesn't appear that a project ever gets selected. On a side note, how would you print out the $selection to see what is going on? Tried print_r in the controller and form and nothing. Thanks.

Terry
#2

[eluser]LuckyFella73[/eluser]
You can set a default entry for your dropdown where
the value is '' - for example:
Code:
$options = array(
                  ''  => 'please select',
                  'value_1'    => 'Cars',
                  'value_2'   => 'Planes',
                  'value_3' => 'Ships',
                );

In your controller set the validation rule for that element like:
$this->form_validation->set_rules('select_name', 'Example Select', 'required');

Don't forget to load the form validation class of course..
#3

[eluser]TerryT[/eluser]
Now the validation is forcing a selection, but despite selecting a project in the dropdown, I get a validation error message stating the Project field is required.

Terry
#4

[eluser]LuckyFella73[/eluser]
If you set the rule only to "required" and get that message than
your selected option has no value or you have an other error - maybe
misspelled something?
Can you show your controller and view script - at least the parts that
are involved in that problem?
#5

[eluser]TerryT[/eluser]
The dropdown only shows the projects that each user is involved in. $data['proj_menu'] feeds the dropdown and works correct. Only an individual user's projects are shown.

Here's the controller part. It is part of the add_item method:

Code:
$this->userid = $this->session->userdata('user_id');
            
//Get only this user's projects:
$data['proj_menu'] = $this->Items_model->get_projects($this->userid, 1);
            
            
//Validation rules. Only required to enter 1 item. Can enter up to 3 items.
$this->form_validation->set_rules('item1', 'Item1', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('item2', 'Item2', 'trim|min_length[2]|xss_clean');
$this->form_validation->set_rules('item3', 'Item3', 'trim|min_length[2]|xss_clean');
$this->form_validation->set_rules('date_worked1', 'Date1', 'trim|required|min_length[7]|xss_clean');
$this->form_validation->set_rules('date_worked2', 'Date2', 'trim|min_length[7]|xss_clean');
$this->form_validation->set_rules('date_worked3', 'Date3', 'trim|min_length[7]|xss_clean');
$this->form_validation->set_rules('proj_select', 'Project', 'trim|required|xss_clean');
            
if ($this->form_validation->run() == FALSE)
{
  $this->load->view('additem_view', $data);
} else {
  $selection = $this->input->post('proj_select');            
                
  if($selection != 0)  //Make sure a project is selected.
{

Here's the view part. :

Code:
<div class='dropdown'>
  <label>Projects</label>
  &lt;?php echo form_dropdown('proj_select', $proj_menu); ?&gt;
</div>


&lt;form method="post" action="&lt;?php echo site_url('Items/add_item')?&gt;" &gt;
    <label>Item 1</label><br/>
    &lt;TEXTAREA Name="item1" rows="3" cols="60" value="&lt;?php echo set_value('item1'); ?&gt;"&gt;&lt;/TEXTAREA><br />
    <label>Date Worked On(mm/dd/yyyy)</label><br/>
    &lt;input type="text" name="date_worked1" value="&lt;?php echo set_value('date_worked1'); ?&gt;" /&gt;&lt;br/>

Terry
#6

[eluser]LuckyFella73[/eluser]
Your select element is not inside your form - that's why there is no POST-value for "proj_select" and it's empty whatever you select.

The rules and all I can see seems to be fine.
#7

[eluser]TerryT[/eluser]
That fixed it! That was a hard lesson over a bunch of hours that won't be forgot. Thanks for all the help. Now if I can figure out how to have the dropdown box maintain the selection when I click into the other input fields, I will be all set. Thanks again.

Terry
#8

[eluser]LuckyFella73[/eluser]
Glad I could help!




Theme © iAndrew 2016 - Forum software by © MyBB