Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter get value from form

Tags:

codeigniter

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.

like image 872
sakkona Avatar asked Dec 30 '10 09:12

sakkona


People also ask

How to GET the value of form input box in CodeIgniter?

=form_input($nm)?> but here its not taking the value of name and id from form_input and not showing it again in form_view in same input box as victor etc so to update and post it back to database...

How to GET form data in controller CodeIgniter?

php class Form_reader extends CI_Controller { public function save_userinput() { //code goes here // for example: getting the post values of the form: $form_data = $this->input->post(); // or just the username: $username = $this->input->post("username"); // then do whatever you want with it :) } } ?>


2 Answers

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);
like image 139
Erik Avatar answered Sep 30 '22 06:09

Erik


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');
like image 21
Vivek Pandey Avatar answered Sep 30 '22 07:09

Vivek Pandey