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
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%)
}
Conclusion:
So, this was all about creating simple contact form using codeigniter framework. Keep following us to learn more.
14 Replies to “How to Create a Contact Form In CodeIgniter”
can u provide the model file..
Replace $this->load->model(‘form_model’); with this line $this->load->helper(‘form’); Thanks
model mainly uses for database operations, there is no need of that
Hello Bhavani,
You can download the script. All the file are available there.
Thanks
FormGet
No Model file required in this tutorial, as this tutorial only show how to built form via codeigniter.
Here is a link of another tutorial that will help you to insert form data via codeigniter.
https://www.formget.com/insert-data-into-database-using-codeigniter/
Download the source code, you will get model file there.
Thanks
Pankaj Agarwal
When i put this code in a file in view…there are shoing some error like this…..
Create Contact Form Using CodeIgniter
‘dname’, ‘name’ => ‘dname’)); ?>
‘demail’, ‘name’ => ‘demail’)); ?>
‘dmobile’, ‘name’ => ‘dmobile’)); ?>
‘daddress’, ‘name’ => ‘daddress’)); ?>
‘submit’, ‘value’ => ‘Submit’)); ?>
.there are shoing some error like this….
( ! ) Fatal error: Call to undefined function form_open() in C:\wamp\www\CI_Form\application\views\form.php on line 7
Call Stack
# Time Memory Function Location
1 0.0010 254608 {main}( ) ..\index.php:0
2 0.0020 312536 require_once( ‘C:\wamp\www\CI_Form\system\core\CodeIgniter.php’ ) ..\index.php:202
3 0.0170 1256752 call_user_func_array:{C:\wamp\www\CI_Form\system\core\CodeIgniter.php:359} ( ) ..\CodeIgniter.php:359
4 0.0170 1256912 Main_controller->index( ) ..\CodeIgniter.php:359
5 0.0180 1262312 CI_Loader->view( ) ..\main_controller.php:11
6 0.0180 1262984 CI_Loader->_ci_load( ) ..\Loader.php:419
7 0.0190 1290912 include( ‘C:\wamp\www\CI_Form\application\views\form.php’ ) ..\Loader.php:833
another error in controller…in this part….could you please help me on that
function__construct(){
parent::__construct()
}
You must add the form helper
function index() {
$this->load->helper(‘form’);
$this->load->view(‘form’);
}
Something like that
thanks a lot. nice script and very simple …
Fatal error: Call to undefined function form_open() in C:xampphtdocscodeig3applicationviewsform.php on line 10
A PHP Error was encountered
Severity: Error
Message: Call to undefined function form_open()
Filename: views/form.php
Line Number: 10
Backtrace:
This is the error i am seeing. please help me to remove thus error.
how to run this form in codeigniter
after some changes ys its work
I encountered
404 Page Not Found
The page you requested was not found.
this error please help me to resolve it
Message: Call to undefined function form_open()
I encountered this as error could you please help me resolve it
your blog is awesome ..its very helpfull form me and there is no video on codeigniter ?