what is GET and POST method in codeigniter?
Form submit can done with two input methods, GET and POST.
CodeIgniter doesn’t support GET method as natively. To use GET method, you have to use normal HTML form
Syntax:
$this->input->post(); // Catch values form url via POST Method
$this->input->get(); // Catch values form url via GET Method
$this->input->get_post(); // Search data first for POST then for GET.
if($this->input->method() == 'post')
{
}
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
}
If( $_SERVER['REQUEST_METHOD'] != 'POST')
{
die();
}
Form Submission Using Post Method
Controller file : form.php
<?php
class Form extends CI_Controller {
public function __construct() {
parent::__construct();
}
// Show form in view page i.e view_page.php
public function form_show() {
$this->load->view("view_form");
}
// When user submit data on view page, Then this function store data in array.
public function data_submitted() {
if($this->input->method() == 'post')
{
$data = array(
'user_name' => $this->input->post('u_name'),
'user_email_id' => $this->input->post('u_email')
);
// Show submitted data on view page again.
$this->load->view("view_form", $data);
}
}
}
?>
View file : view_form.php
<html>
<head>
<title>Codeigniter Form Submit Using Post and Get Method</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css">
</head>
<body>
<div class="main">
<div id="content">
<h3 id='form_head'>Codelgniter Form Submit </h3><br/>
<div id="form_input">
<?php
// Open form and set URL for submit form
echo form_open('form/data_submitted');
// Show Name Field in View Page
echo form_label('User Name :', 'u_name');
$data= array(
'name' => 'u_name',
'placeholder' => 'Please Enter User Name',
'class' => 'input_box'
);
echo form_input($data);
// Show Email Field in View Page
echo form_label('User email:', 'u_email');
$data= array(
'type' => 'email',
'name' => 'u_email',
'placeholder' => 'Please Enter Email Address',
'class' => 'input_box'
);
echo form_input($data);
?>
</div>
// Show Update Field in View Page
<div id="form_button">
<?php
$data = array(
'type' => 'submit',
'value'=> 'Submit',
'class'=> 'submit'
);
echo form_submit($data); ?>
</div>
// Close Form
<?php echo form_close();?>
// Display Entered values in View Page
<?php if(isset($user_name) && isset($user_email_id)){
echo "<div id='content_result'>";
echo "<h3 id='result_id'>You have submitted these values</h3><br/><hr>";
echo "<div id='result_show'>";
echo "<label class='label_output'>Entered User Name : </label>".$user_name;
echo "<label class='label_output'>Entered Email: </label>".$user_email_id;
echo "<div>";
echo "</div>";
} ?>
</div>
</div>
</body>
</html>
CSS File: style.css
body {
font-family: 'Raleway', sans-serif;
}
.main
{
width: 1015px;
position: absolute;
top: 10%;
left: 20%;
}
#form_head
{
text-align: center;
background-color: #FEFFED;
height: 66px;
margin: 0 0 -29px 0;
padding-top: 35px;
border-radius: 8px 8px 0 0;
color: rgb(97, 94, 94);
}
#content {
position: absolute;
width: 450px;
height: 356px;
border: 2px solid gray;
border-radius: 10px;
}
#content_result{
position: absolute;
width: 450px;
height: 192px;
border: 2px solid gray;
border-radius: 10px;
margin-left: 559px;
margin-top: -262px;
}
#form_input
{
margin-left: 50px;
margin-top: 36px;
}
label
{
margin-right: 6px;
font-weight: bold;
}
#form_button{
padding: 0 21px 15px 15px;
position: absolute;
bottom: 0px;
width: 414px;
background-color: #FEFFED;
border-radius: 0px 0px 8px 8px;
border-top: 1px solid #9A9A9A;
}
.submit{
font-size: 16px;
background: linear-gradient(#ffbc00 5%, #ffdd7f 100%);
border: 1px solid #e5a900;
color: #4E4D4B;
font-weight: bold;
cursor: pointer;
width: 300px;
border-radius: 5px;
padding: 10px 0;
outline: none;
margin-top: 20px;
margin-left: 15%;
}
.submit:hover{
background: linear-gradient(#ffdd7f 5%, #ffbc00 100%);
}
.label_output
{
color:#4A85AB;
margin-left: 10px;
}
#result_id
{
text-align: center;
background-color: #FCD6F4;
height: 47px;
margin: 0 0 -29px 0;
padding-top: 12px;
border-radius: 8px 8px 0 0;
color: rgb(97, 94, 94);
}
#result_show
{
margin-top: 35px;
margin-left: 45px;
}
.input_box{
height:40px;
width:240px;
padding:20px;
border-radius:3px;
background-color:#FEFFED;
}
Form Submission Using Get Method
CodeIgniter doesn’t support GET method natively. So we have to use normal HTML form code.
<form action="form/data_submitted" method="get">
User Name :: <input type="text" name="u_name" placeholder="Please Enter User Name" class="input_box">
User email: <input type="text" name="u_email" placeholder="Please Enter Email Address" class="input_box">
<input type="submit" value="Submit" class="submit">
</form>
Values receive as array using GET in Controller.
public function data_submitted()
{ $data = array(
'user_name' => $this->input->get('u_name'),
'user_email_id' => $this->input->get('u_email')
);
Input Class
input->get()
input->post()
input->get_post()
input->cookie()
input->server()
input->input_stream()
input->get_request_header()
$this->input->get()
$this->input->get('some_data', TRUE);
To return an array of all GET items call without any parameters.
To return all GET items and pass them through the XSS filter set the first parameter NULL while setting the second parameter to boolean;
The function returns FALSE (boolean) if there are no items in the GET.
$this->input->get(NULL, TRUE); // returns all GET items with XSS filter
$this->input->get(); // returns all GET items without XSS filtering
$this->input->post()
$this->input->post('some_data');
The function returns FALSE (boolean) if the item you are attempting to retrieve does not exist.
The second optional parameter lets you run the data through the XSS filter. It's enabled by setting the second parameter to boolean TRUE;
$this->input->post('some_data', TRUE);
To return an array of all POST items call without any parameters.
To return all POST items and pass them through the XSS filter set the first parameter NULL while setting the second parameter to boolean;
The function returns FALSE (boolean) if there are no items in the POST.
$this->input->post(NULL, TRUE); // returns all POST items with XSS filter
$this->input->post(); // returns all POST items without XSS filter
$this->input->get_post()
This function will search through both the post and get streams for data, looking first in post, and then in get:
$this->input->get_post('some_data', TRUE);
$this->input->ip_address()
Returns the IP address for the current user. If the IP address is not valid, the function will return an IP of: 0.0.0.0
echo $this->input->ip_address();
$this->input->get_request_header();
Returns a single member of the request headers array.
$this->input->get_request_header('some-header', TRUE);
Strip_tag method
File Name :
<?php
$product_title = strip_tags($this->input->post('product_title'));
$product_name = strip_tags($this->input->post('product_name'));
$product_type = strip_tags($this->input->post('product_type'));
$listing_type = strip_tags($this->input->post('listing_type'));
$category = strip_tags($this->input->post('category'));
$sub_category = strip_tags($this->input->post('sub_category'));
?>
Previous
Next