C

Simplified tutorials to learn the popular basic language C

VB.Net

Learn the basic desktop development application language

CodeIgniter

One of the popular PHP three tier (MVC) framework

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.

Loading View Pages into the Controller


In our previous article, we have created pages in CodeIgniter. But we don’t write anything directly on the Controller section except PHP codes, and we have to write those HTML section in View part.



Creating a View Page

  • Just open your CodeIgniter View folder.
  • There can see two files, which are default loading files.You might delete these files, 
  • Here we have to create our View file.
  • So create a PHP page or HTML page. You need PHP page for loading PHP scripts on the view section.
  • Here I create a PHP file with the name ‘registration.php’.
Creating View file in CodeIgniter

You can write any HTML, PHP, CSS, JavaScript codes inside this page.

Let us write some basic HTML tags:

<html>
<head>
<title>Registration</title>
</head>
<body bgcolor="red">
 Register:
</body>
</html>

How to load a View Page to a Controller Page?

It is very easy to load a view page to the controller.
  • Just open your controller file, there we have already written many pages(functions).
  • Now we are going to load our View page to our controller register page.
  • So just write down below code inside the ‘register()’ page function

    $this->load->view("registration");
This line means that, this controller page is loading a view page named registration that saved on view folder.


Loading a View file to controller in CodeIgniter

Let us run the page register:
Then result should like this:

Running CodeIgniter View page

Like this, we can create corresponding view page for ‘login’.
Then you have to connect to the login controller

           $this->load->view("login");

Here the name of view page should be ‘login’. That may be login.php or login.html, if both are exist, then you need to specify its format like  $this->load->view("login.php");

Loading a View file to controller

Note:

In your project, there may be hundreds of view pages. So saving all view pages in a single folder is difficult. So you can categorize view files as folders.
For example:

Create a folder to keep all view files related to user and creating another folder for keeping all view files related to admin. (where both user and admin are the major modules in the project)

Now I just put my login.php into admin folder and registration.php into user folder.

Loading a View file to controller

So, in your controller, you must specify the name of folder with view file.

<?php
class User extends CI_Controller
 {
  public function index()
  {

  }
 public function login()
  {
    $this->load->view("admin/login");
  }
 public function register()
  {
    $this->load->view("user/registration");
  }
 }

Categorizing views as folder in CodeIgniter

Loading Multiple View pages

In a large project, your header and footer sections will be same in all your pages. Then you can make them common. If you make a separate files for header and footer then you can load those file together with your main file.

Here are 3 files.
(You can write anything on these files)

header.php
<html>
<head>
<title>Registration</title>
</head>
login.php
<body bgcolor="#53aaac">
 Login here
footer.php
<hr/>
<footer>This is the page footer</footer>
</body>
</html>

Now your login controller can load all these files together
public function login()
  {
     $this->load->view("admin/header");
     $this->load->view("admin/login");
     $this->load->view("admin/footer");
  }

Then the out will be combination of all these view files.

Loading Multiple views in CodeIgniter

We will learn more in coming lessons...

Creating pages using Functions in CodeIgniter


OK.. Let’s start the coding…

Before starting the coding, you must read the basics of the CodeIgniter frameworks those written in our previous articles.


Creating Controller

  • Now just open your Controllers folder.
CodeIgniter Controllers folder

  • There you can see two files names as ‘index.html’ and ‘welcome.php’. These are two default file to load. Just delete these files. 
  • Now let us create a new PHP file. You must aware of basic concepts of plane PHP such as creating PHP files, basic PHP coding, etc. 
  • Right click here and from context menu, select New-> Text document. And save the file with extension of ‘.php’. 
  • Here created a PHP file with the name ‘user.php’.
Now let us open the file. Here Codelopster is used to open and edit PHP files. You can use any text editor even notepad, but Codelopster, sublime or Atom are free and recommended PHP text editors.

Inside the file, you have to create a Controller class like this:

<?php
class User extends CI_Controller
 {

 }
  • Class should be written after a PHP opening tag (<?php)
  • Class name should be equal to the file name. Here name of both class name and file name are ‘user’. But you need to capitalize the first letter of the class name (User). 
  • You have to inherit the parent class CI_Controller by using keyword ‘extends’. 
  • You don’t need to write the php closing tag (?>). 
Done. Now you have completed the first process of creating a controller class.

Creating PHP pages

How do we create PHP pages in plane php? There we have to create separate php files for each pages on the project. But here, you don’t need to create any pages.
In CodeIgniter, every functions on the controller class act as separate and independent pages.
So if we want to create a PHP page, then just write a function with the name of the page.

Let us create a page with the name ‘index’.
  • First of all create a function with the name ‘index’ and you have to specify the type of the function, here which is ‘public’. 
  • Function should ends with a parenthesis ().
  • The function should contain a body section with brace brackets {}.
  • The function should be inside the class (inside class brace brackets).
  • As a testing, let’s print ‘hello’ using echo. 

<?php
class User extends CI_Controller
 {
  public function index()
   {
    echo "hello";
   }
 }

Creating a CodeIgniter Controller class file

Let us run the project...

We have learnt already in our first lesson about running a CodeIgniter project.
So we need to specify the name of controller and the name of the page (function) that we want to run.

CodeIgniter first output page

Like this, you can create many functions according the number of pages.
Here let us create two another pages named as ‘register’ and ‘login’.

<?php
class User extends CI_Controller
 {
  public function index()
  {
  echo "hello";
 }

 public function login()
  {
  echo "Login here";
 }

 public function register()
  {
    echo "New? Register";
 }
 }

If you want to open your 'register' page, then browser url should be like this:

http://localhost/myproject/index.php/user/register

Running a function in codeigniter

Note: 

I think you have already aware about the name ‘index’. The page (function) with the name ‘index’ will automatically run if we do not specify any page (function) name.
That is, if we run this URL, http://localhost/myproject/index.php/user(here any name of page or function is not specified), then PHP automatically run the page (function) named index
Then the URL
http://localhost/myproject/index.php/user/index will work automatically. 

Now we just learnt how to create pages in CodeIgniter. Yeah, It’s very easy…

Here we wrote all the texts (‘hello’, ‘login here’, ‘New? Register’) inside the controllers' pages. But we never write any text or HTML codes inside the controller page. Controller page is only to write PHP codes. In previous lesson we leant that the ‘View’ section is used to write these type of text and HTML tags.

So let us learn those ideas in our next lesson.. 

MVC Architecture of the CodeIgniter Framework

In previous lesson, we have completed the CodeIgniter installation. Now lets us look at their architecture. As we shown in previous lesson, mainly there are three core folders named as Controllers, Models and Views. These three folders contain the main files for building our CodeIgniter project.

How to Create and Run a CodeIgniter Project


Tip Abstraction:
          This is an introduction tutorial for one of the popular PHP framework CodeIgniter.

Working with Button and Click Event in VB


Tip Abstraction:
        This visual basic tutorial describes the working of button element and its most relevant event

Declaring and Using Variables in VB (Dim)


Tip Abstraction:
       This visual basic tutorial describes how to declare and use a variable with assigning values