Press "Enter" to skip to content

How to Send a Form Value from View to Controller – CodeIgniter

Sending Form Values from View to Controller

Now we created many pages on our project. But we have to do something with our project.
We already leant about the data flow in CodeIgniter MVC. There are mainly 6 ways of data flows.

Here we are discussing about the first step.

Posting a data to controller in CodeIgniter

That is sending a data from our view file into the Controller page.

Like normal plane PHP, usually we send form values into the next page.
So let us create a simple form in our view section.

<html>
<head>
<title>Registration</title>
</head>
<body bgcolor="#b1ac4e">
 Register here:
 <form action="userregister" method="post" >
  Name: <input type="text" name="username"/></br>
  Place:<input type="text" name="userplace"/></br>
  Age:  <input type="number" name="userage"/></br>
        <input type="submit" value="Register"/>
 </form>
</body>
</html>

 

View page in CodeIgniter

Almost same as to plane PHP, but look at form action.

<form action=”userregister” method=”post” >

Here userregister is the function name that you need to post your input data on controller. So you must have controller page with the name ‘userregister’.

While you click on the submit button, then all your input values named ‘username’, ‘userplace’ and ‘userage’ will be send to the controller page ‘userregister’.

So we have to write codes on controller page ‘userregister’ to catch sent values from the View page ‘registration.php’.

For Example:

  public function userregister()
  { 
    $this->input->post("username");
  }

Here, $this->input->post(“username”); This line of code retrieve the value named ‘username’. Where keyword post used because our data transfer method was post in the view page.

Send form value into controller in codeIngniter

Post method is always good and secure and the method get is not secure because your transmitting value will be on page URL. And you can use the get method in necessary conditions. We will learn them later.

If your method is get, then your code will be:

$this->input->get("username");

As a testing, let us print the value of posted data on controller.
Let us store the posted value into a variable.

public function userregister()
  { 
   $name =  $this->input->post("username");
   echo $name;
 }

or we can directly print the posted data:

 echo $this->input->post("username");

like username, you can send all other values such as userplace and userage to the controller.

$place = $this->input->post("userplace");
$age = $this->input->post("userage");

Now the total code:

<?php
class User extends CI_Controller
 {
  public function register()
  {
    $this->load->view("user/registration");
  }
  public function userregister()
  { 
    $name = $this->input->post("username");
    $place = $this->input->post("userplace");
    $age = $this->input->post("userage");
    echo $name." ".$place." Age is ".$age;
   }
 }

Sample Output:

Sample output example in codeIgniter

Like this, you can send any values such as text, number, password, etc. depending the input forms on the view file.

Be First to Comment

Leave a Reply