8

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.

  1. how can i correct this problem?
  2. 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.

3 Answers 3

10

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I have replaced like tihs: <html> <head><title>Test Example View</title></head> <body> <h1 align="center">Test Sample CI Framwork</h1> <?php echo form_open('example/getvalue'); ?> <?php $data = array('name' => 'username','value' => $this->input->post('username')); echo form_input($data); ?> <div> <?php echo form_submit(array('name'=>'submit'), 'Submit!');?></div> <?php echo form_close(); ?> </body> </html>
4

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');

Comments

0

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!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.