CodeIgniter is a framework based on MVC (Model, View & Controller) architecture. It includes step by step process to construct PHP applications.



In this tutorial, we will show, how to create a simple contact form using CodeIgniter framework as follows:


  • In first step, you must create a php page in View directory of codeigniter, in which a form need to be created using codeigniter’s syntax.
<?php echo form_open('main_controller'); ?>
<?php echo form_label('Student Name :'); ?>
.............
<?php echo form_close(); ?>

 


  • Secondly, you have to create class in Controller directory, to load  above php page(view) and to call other functions.
<?php
class main_controller extends CI_Controller {
..............
$this->load->view('form');
........
?>

 


  • And then  if there is a need of database connectivity in your application then, a class needs to be created in Model directory with database functions.  Here, we do not require this class, as we are simply creating a form.

We have given below our codes you can copy that to your view and controller files to use it.

or

Download the create_codeigniter_form.zip file from below link, extract files and include them in view and controller directory of your codeigniter framework as illustrated in the Read Me.txt file.

 


 See Also  Form Validation Using CodeIgniter 

create contact form using codeigniter

 


VIEW FILE: form.php

Copy the below code in your view file.

<!DOCTYPE html>
<html>
<head>
<title>Create Contact Form Using CodeIgniter</title>
<link href='http://fonts.googleapis.com/css?family=Marcellus' rel='stylesheet' type='text/css'>
<link href="http://localhost/CodeIgniter/css/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="container">
<?php echo form_open('main_controller'); ?>
<h1>Create Contact Form Using CodeIgniter</h1>
<?php echo form_label('Student Name :'); ?>
<?php echo form_input(array('id' => 'dname', 'name' => 'dname')); ?>
<?php echo form_label('Student Email :'); ?>
<?php echo form_input(array('id' => 'demail', 'name' => 'demail')); ?>
<?php echo form_label('Student Mobile No. :'); ?>
<?php echo form_input(array('id' => 'dmobile', 'name' => 'dmobile')); ?>
<?php echo form_label('Student Address :'); ?>
<?php echo form_input(array('id' => 'daddress', 'name' => 'daddress')); ?>
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
<?php echo form_close(); ?>
</div>
</body>
</html>

 

CONTROLLER FILE: main_controller.php

Copy the below code in your controller file.

<?php
class main_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('form_model');
}
function index() {
$this->load->view('form');
}
}
?>

 

CSS FILE: styles.css

Styling HTML Elements.

#container{
width:960px;
height:610px;
margin:50px auto
}
#fugo{
float:right
}
form{
width:320px;
padding:0 50px 20px;
background:linear-gradient(#fff,#ABDBFF);
border:1px solid #ccc;
box-shadow:0 0 5px;
font-family:'Marcellus',serif;
float:left;
margin-top:10px
}
h1{
text-align:center;
font-size:28px
}
hr{
border:0;
border-bottom:1.5px solid #ccc;
margin-top:-10px;
margin-bottom:30px
}
label{
font-size:17px
}
input{
width:100%;
padding:10px;
margin:6px 0 20px;
border:none;
box-shadow:0 0 5px
}
input#submit{
margin-top:20px;
font-size:18px;
background:linear-gradient(#22abe9 5%,#36caf0 100%);
border:1px solid #0F799E;
color:#fff;
font-weight:700;
cursor:pointer;
text-shadow:0 1px 0 #13506D
}
input#submit:hover{
background:linear-gradient(#36caf0 5%,#22abe9 100%)
}

Pabbly Form Builder


Conclusion:

So, this was all about creating simple contact form using codeigniter framework. Keep following us to learn more.