codeigniter get value from form
I am a beginner codeigniter. My purpose is get value from form of example_view.php view, then display this result in the result.php view, when i submitted.
- how can i correct this problem?
- If i want to pass this value into session, how can i do?
example_view.php(view)
<html>
<head><title>Test Example View</title></head>
<body>
<h1 align="center">Test Sample CI Framwork</h1>
<?php echo form_open('example/getvalue'); ?>
<input type="text" name="username" value="" size="50" />
<div><input type="submit" value="Submit" name="submit"/></div>
<?php echo form_close(); ?>
</body>
</html>
result.php(view)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head开发者_开发问答>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php echo 'Your new value is '.$value;?>
</body>
</html>
example.php(controller)
<?php
class Example extends Controller {
function Example()
{
parent::Controller();
$this->load->helper('form');
$this->load->helper('url');
}
function index() {
$this->load->view('example_view');
$this->getvalue();
}
function getvalue()
{
if ($this->input->post('submit')==true) {
$data['value']=$this->input->post('username');
$this->load->view('result',$data);
}
}
}
?>
Thank for your help.
If you are using form_validation too, you can use
set_value('test'); // $_POST['test']
Otherwise use
$this->input->post('test'); // $_POST['test']
In your form, set something like;
$data = array('name' => 'field' => 'value' => $this->input->post('test'));
echo form_input($data);
To get the value in codeigniter from input you can use post
$this->input->post('name');
You can also get the value from a GET method
$this->input->get('name');
I've copied and pasted your code, and everything seems to be working fine for me - I'm a little unclear as to what your problem under "1." is?
As for sessions, the CodeIgniter guide is pretty useful for this: http://codeigniter.com/user_guide/libraries/sessions.html
Essentially, you need to load the session library first with
$this->load->library('session');
Then you can set session data with
$this->session->set_userdata('some_name','some_value');
So your final function would look something like this
function getvalue()
{
if ($this->input->post('submit')==true) {
$data['value']=$this->input->post('username');
$this->session->('username',$data['value']);
$this->load->view('result',$data);
}
}
You can then retrieve the data by calling $this->session->userdata('item'), so for your code
echo $this->session->userdata('username');
should output whatever has been put into the textbox (but don't forget to run some security checks on the input box first!)
精彩评论