Welcome Guest, Not a member yet? Register   Sign In
get selected text and selected value from select box in controller ?
#1

[eluser]thaodth[/eluser]
I have a form. I want get value selected and text selected from select box, but $this->inpust->post() only get value, I want get text selected, too.
Code HTML:
Code:
<form id="frmAddHostInfo" class="validate" method="post" action="<?php echo $base_url?>dashboard/insert_new_host">
        <div class="content no-padding">
         <div class="section _100">
          <label>
           Host name
          </label>
          <div class="_100">
           &lt;input class="required" name="hostname" id="hostname"&gt;
          </div>
         </div>
         <div class="section _100">
          <span class="label">In group</span>
          <div class="_100">
           <p>
            <select id="group" name="group" class="required"> &lt;!-- class "required" used in jquery.validate.js --&gt;
             <option></option>
             &lt;?php foreach($groups as $groupid=>$grpname) { ?&gt;
              <option value="&lt;?php echo $groupid; ?&gt;">&lt;?php echo $grpname;?&gt;</option>
             &lt;?php } ?&gt;
            </select>
           </p>
          </div>
         </div>
</div>
&lt;/form&gt;

Code in controller:
Code:
public function insert_new_host()
{
  //$hostname, $os, $ip, $groupid, $groupname, $pid, $pname, $departmentid, $dname
  $hostname = $this->input->post('hostname');
// $groupid = ?? //how to get?
//$groupname = ??? //how to get?
        }

Please show me how to solve this problem!
#2

[eluser]Pert[/eluser]
ID
Code:
$groupid = $this->input->poist('group');

Name
Code:
$groups = ... ; // how ever you create groups for the view file
$groupname = $groups[$groupid];
#3

[eluser]thaodth[/eluser]
[quote author="Pert" date="1370875892"]ID
Code:
$groupid = $this->input->poist('group');

Name
Code:
$groups = ... ; // how ever you create groups for the view file
$groupname = $groups[$groupid];
[/quote]

Oke. So it's meaning I must repeat the steps which I created groups in controller?
Code is used to create groups for view file:
Code:
$groups = $this->model->get_group_data();  //repeat it!
foreach($groups as $index=>$row) {                                               //repeat it!
   $list_groups[$row->groupid] = $row->name; //repeat it!
  } //repeat it!
$this->load->view('dashboard', array('base_url'=>$this->config->item('base_url'),
            'groups' => $list_groups);


am i right?
#4

[eluser]greedyman[/eluser]
There is a simple way to do. To get value of Id, you can $groupid = $this->input->post('group'); and then in your controller you should get groupname by using $query = $this->db->query() ... etc. You don't need get group name from your view, it harder than.

P/S: Do you know VN?
#5

[eluser]Pert[/eluser]
[quote author="thaodth" date="1370918913"]
am i right?[/quote]

If you have method in your model to get a single group, you don't have to repeat your code.

Then you would end up with something like this:
Code:
$group = $this->model->get_group_data($this->input->post('group'));
echo $group->name;
#6

[eluser]thaodth[/eluser]
[quote author="Pert" date="1370937901"][quote author="thaodth" date="1370918913"]
am i right?[/quote]

If you have method in your model to get a single group, you don't have to repeat your code.

Then you would end up with something like this:
Code:
$group = $this->model->get_group_data($this->input->post('group'));
echo $group->name;
[/quote]
Oke. thanks Pert. Smile
#7

[eluser]thaodth[/eluser]
[quote author="greedyman" date="1370920351"]
P/S: Do you know VN?[/quote]
Hi greedyman, what does your postscript mean? I'm Vietnamese.
#8

[eluser]greedyman[/eluser]
Did you do?

[quote author="thaodth" date="1370939317"][quote author="greedyman" date="1370920351"]
P/S: Do you know VN?[/quote]
Hi greedyman, what does your postscript mean? I'm Vietnamese. [/quote]

Ok, I see. I'm sure you will recognize that words. Nice to meet you! I come from HN.
#9

[eluser]thaodth[/eluser]
[quote author="greedyman" date="1370957063"]Did you do?
Ok, I see. I'm sure you will recognize that words. Nice to meet you! I come from HN.[/quote]

Nice to meet u, greedyman ^^. I come from Châu Đốc, An Giang. Both solutions are good, but I chose the second solution. Because I won't repeat code to create group in view file.
#10

[eluser]greedyman[/eluser]
[quote author="thaodth" date="1370964379"][quote author="greedyman" date="1370957063"]Did you do?
Ok, I see. I'm sure you will recognize that words. Nice to meet you! I come from HN.[/quote]

Nice to meet u, greedyman ^^. I come from Châu Đốc, An Giang. Both solutions are good, but I chose the second solution. Because I won't repeat code to create group in view file.[/quote]

Ok. Choose the best solution for you!




Theme © iAndrew 2016 - Forum software by © MyBB