Welcome Guest, Not a member yet? Register   Sign In
How to access a variable in a view from a controller??
#1

[eluser]flyingfish[/eluser]
Hi All,

I'm new to CI and as a project to get my feet wet Im rewriting a small app to use the CI framework. My question is how do I access the value of a input variable in a view from the controller of that view?

Thanks for any examples!

cheers!
davej
#2

[eluser]bubbafoley[/eluser]
http://ellislab.com/codeigniter/user-gui...views.html
#3

[eluser]flyingfish[/eluser]
Hi bubbafoley,

Thanks for your response. Basically I have a logon screen and want to access the values entered by the user so I can validate them against a database of users.

So my view file looks like:
[code above here]
<td width="286" align="left" valign="top">
<label>
&lt;input name="username" type="text" id="username" maxlength="16" value="&lt;?php echo set_value('username'); ?&gt;" /&gt;
</label>&nbsp;&lt;?php echo form_error('username'); ?&gt;
</td>
</tr>
<tr>
<td align="left" valign="top"><strong>Password:</strong></td>
<td align="left" valign="top">
<label>
&lt;input type="password" name="password" id="password" value="&lt;?php echo set_value('password'); ?&gt;" /&gt;
</label>&nbsp;&lt;?php echo form_error('password'); ?&gt;
</td>
</tr>
[code below here]

and my controller looks like:
[code above here]
$this->load->database();

//set validation ruels
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|max_length[16]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required');

if ($this->form_validation->run() == FALSE)
{
$this->load->view('index');
}
else
{
//check details and give login page if all ok
$sql = "SELECT iduser FROM fmg_user WHERE (status = 'A') AND username = ? AND password = ?";
$result = $this->db->query($sql, array([username need to be here],[passwrd needs to be here]));
[code below here]

How to I get the username and password values from the input variables into code above?

cheers!
davej
#4

[eluser]bubbafoley[/eluser]
ah, i see now. use $this->input->post() to get form data

Code:
$username = $this->input->post('username');
$password = $this->input->post('password');
$sql = "SELECT iduser FROM fmg_user WHERE (status = ‘A’) AND username = $username AND password = $password";
result = $this->db->query($sql);

OR you can use CI's active record

Code:
$result = $this->db->select('iduser')
  ->where('username', $this->input->post('username'))
  ->where('password', $this->input->post('password'))
  ->get('fmg_user');
#5

[eluser]CroNiX[/eluser]
The first manual example above is not safe as you are not escaping your input. AR does it automatically.

$var = $this->db->escape($var);
#6

[eluser]flyingfish[/eluser]
Ok, thanks to you both!




Theme © iAndrew 2016 - Forum software by © MyBB