Welcome Guest, Not a member yet? Register   Sign In
getting the select value
#1

[eluser]_Moat_[/eluser]
hello there,
i'm trying to get the value of the selected option on a drop down list.

what i do is:
view.php->
Code:
<select name="ID">
           <option value="0" >Chose a Category</option>
                    &lt;?php $i = 0; foreach ($frm5_ctg as $key) { ?&gt;
                    <option name ="test" value="&lt;?php echo set_value() == "" ? $frm5_ctg[$i]['ID'] : set_value('test');?&gt;">&lt;?php echo $frm5_ctg[$i]['CATEGORY_NAME'];?&gt;</option>
                   &lt;?php $i++;} ?&gt;
              </select>

next at the cntrler:
contrlr.php->
Code:
$data['frm5_ctg'] = $this->edit_page_db->getPageInfo("1,2,4,5,6,8,9,10");
$category = $this->input->post('ID');
$this->load->view('view', $data);
model->
Code:
public function getPageInfo($parentID)
{
  $sql = "select ID, CATEGORY_NAME from igs_page_category where CATEGORY_PARENT_ID in ($parentID)";
  $query = $this -> db -> query($sql,$parentID);
  if ($query -> num_rows() > 0)
   {
   foreach ($query->result_array() as $row)
   {
    $ctg_ids[] = $row;
    }
   return $ctg_ids;
  }
}
no when it loads it gives me the correct "ID's" from the db but the post value $category=0
so how can i retrieve the correct value.
#2

[eluser]InsiteFX[/eluser]
This needs to be changed to.
Code:
<option name ="test" value="&lt;?php echo set_value() == '' ? $frm5_ctg[$i]['ID'] : set_value('test');?&gt;">&lt;?php echo $frm5_ctg[$i]['CATEGORY_NAME'];?&gt;</option>
#3

[eluser]CroNiX[/eluser]
Only form selects have "names", not their options.

Also,
Code:
set_value('test')
should be the name of your select element...so it would be
Code:
set_value('ID')

There are also form helpers for creating a dropdown, which makes this a lot easier than your manual way.
#4

[eluser]_Moat_[/eluser]
my mistake i did post an old code
here is the code which still on post gives me 0 for the $category
Code:
<select name="ID">
                   <option>Chose a Category</option>
                    &lt;?php $i = 0; foreach ($frm1_ctg as $key) { ?&gt;
                    <option value="&lt;?php echo set_value() == "" ? ($frm1_ctg[$i]['ID']) : set_value("ID") ?&gt;">&lt;?php echo $frm1_ctg[$i]['CATEGORY_NAME'];?&gt;</option>
                   &lt;?php $i++;} ?&gt;
               </select>
#5

[eluser]CroNiX[/eluser]
Code:
public function getPageInfo($parentID)
{
  $sql = "select ID, CATEGORY_NAME from igs_page_category where CATEGORY_PARENT_ID in ($parentID)";
  $query = $this -> db -> query($sql,$parentID);
  if ($query -> num_rows() > 0)
  {
   foreach ($query->result_array() as $row)
   {
    $ctg_ids[$row['ID'] = $row['CATEGORY_NAME'];  //changed this
   }
   return $ctg_ids;
  }
}
Code:
//in the view
echo form_dropdown('ID', $ctg_ids, set_value('ID', 0));
will create the dropdown, it's options, select the current value (if form was submitted), or select whatever has the value of 0 if the form hasn't been submitted (a default value, like "Choose ID")
#6

[eluser]_Moat_[/eluser]
CroNiX,
just to be clear, with my code i was able to fill the options from db without a problem, but when i submit the form and try to catch the selected option it gives me 0.

now i tried what u suggested but still the post of the 'ID' => 0
and the variable in
Code:
echo form_dropdown('ID', $ctg_ids, set_value('ID'), 0);
this line should be
Code:
echo form_dropdown('ID', $frm1_ctg, set_value('ID'), 0);
since i store it in my controller at $frm1_ctg

thnx for your help.
#7

[eluser]CroNiX[/eluser]
Looking at the raw html generated might provide some clues. I'm sure your way worked fine; I was just offering a cleaner alternative CI provides.

Post your current code.
#8

[eluser]_Moat_[/eluser]
i'll try my best to include what ever piece of code required.

controller:
Code:
public function Index()
{
$query = array();
$data['frm1_ctg'] = $this->edit_page_db->getPageInfo("4");
if ($this->input->post('form1'))
{
        $business = $this->input->post('business_f1');
$street = $this->input->post('street');
$city = $this->input->post('city');
$postal = $this->input->post('postal');
$phone = $this->input->post('phone');
$category = $this->input->post('ID');
$query = array('PAGE_NAME'=> $business, 'PAGE_CATEGORY_ID'=> null,'OFFICIAL_PAGE'=> null,'START_DATE'=>null,'STREET_ADDRESS'=>$street,'CITY_TOWN_NAME'=>$city,'CITY_TOWN_ID'=>null, 'POSTCODE'=>$postal, 'ABOUT' => null, 'COMPANY_OVERVIEW' => null, 'DESCRIPTION' => null, 'MISSION' => null, 'AWARDS'=>null, 'PRODUCTS'=>null, 'PHONE' => $phone, 'EMAIL'=>null, 'WEBSITE' => null);
echo "$business . $street . $city . $postal . $phone . $category ";
//  Businnes Or Place Street Adress City/Country Postal Code Phone 0 "HERE"
if ($business != "Business or Places" && $street != "Street Adress" && $city != "City/Country" && $postal != "Postal Code" && $phone != "Phone"  )
{  
     $this->form_validation->set_rules('ID','Category','required');
     $this->form_validation->set_rules('business_f1','Business or Places','required');
     $this->form_validation->set_rules('street','Street Adress','required');
     $this->form_validation->set_rules('city','City or Country','required');
     $this->form_validation->set_rules('postal','Postal Code','required');
     $this->form_validation->set_rules('phone','Phone','required');  
}
} // end of if ($this->input->post('form1'))
if (!$this->form_validation->run())
  {
   $data['message_error'] =  validation_errors();  
   $this->load->view('create_page',$data);
  }
  else
  {
   $this->edit_page_db->saveInfo($query);
   $this->load->view('edit_page_info',$data);
  }
} //end of index

view:

Code:
<select name="ID">
<option>Chose a Category</option>
&lt;?php $i = 0; foreach ($frm1_ctg as $key) { ?&gt;
<option value="&lt;?php echo set_value() == "" ? ($frm1_ctg[$i]['ID']) : set_value("ID") ?&gt;">&lt;?php echo $frm1_ctg[$i]['CATEGORY_NAME'];?&gt;
</option>
&lt;?php $i++;} ?&gt;
</select>
&lt;input id="business_f1" name ='business_f1' value="&lt;?php echo set_value() == "" ? "Businnes Or Place" : set_value("business"); ?&gt;" type="text" class="stretch"&gt;
&lt;/input&gt;
&lt;input id="street" name ='street' value="&lt;?php echo set_value() == "" ? "Street Address" : set_value("street"); ?&gt;" type="text" class="stretch"&gt;
&lt;/input&gt;
&lt;input id="city" name = 'city' value ="&lt;?php echo set_value() == "" ? "City/Country" : set_value('city'); ?&gt;" type="text" class="stretch"&gt;
&lt;/input&gt;
&lt;input id="postal" name = "postal" value="&lt;?php echo set_value() == "" ? "Postal Code" : set_value('postal'); ?&gt;" type="text" class="stretch"&gt;
&lt;/input&gt;
&lt;input id="phone" name = "phone" value="&lt;?php echo set_value() == "" ? "Phone" : set_value('phone'); ?&gt;" type="text" class="stretch"&gt;
&lt;/input&gt;
&lt;input value="true" type="checkbox" id="checkbox_text_f1"&gt;
<div id="checkbox_text">I agree to</div>
<a href="/" class="igotshare_terms">IGotShare Pages Terms</a>
&lt;/input&gt;
&lt;input type="submit" class="submit_btn" value="Get Started" name="form1"&gt;

model:

Code:
class edit_page_db extends CI_Model
{
function __construct()
  {
  $this -> load -> database();
  }
public function saveInfo($query)
{
  $this -> db -> insert('igs_page', $query);
}

public function getPageInfo($parentID)
{
  $sql = "select ID, CATEGORY_NAME from igs_page_category where CATEGORY_PARENT_ID in ($parentID)";
  $query = $this -> db -> query($sql,$parentID);
  if ($query -> num_rows() > 0)
   {
   foreach ($query->result_array() as $row)
   {
                         $ctg_ids[] = $row;
    }
   return $ctg_ids;
  }
}


this is all my old code nothing changed
#9

[eluser]CroNiX[/eluser]
Need to see the raw html of your select (not the php code to produce it). Just go to the page, right click and "view source" or similar depending on your browser.
#10

[eluser]CroNiX[/eluser]
And this is still wrong:
Code:
<option value="&lt;?php echo set_value() == "" ? ($frm1_ctg[$i]['ID']) : set_value("ID") ?&gt;">&lt;?php echo $frm1_ctg[$i]['CATEGORY_NAME'];?&gt;

set_value() expects a parameter, the name of the form element. You can't use
Code:
if (set_value() == '')
, which you seem to be using all over the place. I'd read up on set_value().




Theme © iAndrew 2016 - Forum software by © MyBB