Add Codeigniter validations to web application
We use codeigniter validations to avoid wrong inputs, most of the web applications face security issues, and codeigniter provides security functions like XSS (cross site scripting), which escapes inputs from users and provides security from cross site scripting vulnerability.
Let’s create a view in codeigniter application and add validations using codeigniter form validations.
<div class="form-group"> <?php if($this->session->flashdata('message')) {?> <label><span style="color:red"><?php echo $this->session->flashdata('message');?></span></label> <?php }?> </div> <?php if($query->num_rows()>0) { $row = $query->row(); ?> <div> <form method="post" action="edit_user_profile" id="theform" > <div class="white-shadow content-inner-block"> <span style="font-weight:normal;color:#333333;text-align:right"> <font color="red">*</font> Marked fields are compulsory </span> </div> <div class="form-group"> <label for="txtLoginid">Name <span class="style1">*</span> </label> <input name="txtName" type="text" size="25" id="txtName" placeholder="First Name" class="form-control" value="<?php echo $row->firstname;?>" /> <span id="nameError" class="red"></span> <span style="color:red"><?php echo form_error('txtName');?></span> </div> <div class="form-group"> <label for="txtLoginid">Email <span class="style1">*</span> </label> <input name="txtEmail" type="email" size="25" id="txtEmail" placeholder="Enter email" class="form-control" value="<?php echo $row->emailid;?>" readonly /> <span id="emailError" class="red"></span> <span style="color:red"><?php echo form_error('txtEmail');?></span> </div> <div class="form-group"> <label for="txtcompanyname">Company Name</label> <input name="txtcompanyname" type="text" size="25" id="txtcompanyname" placeholder="Enter Company name" class="form-control" value="<?php echo $row->company_name;?>" /> </div> <div class="form-group"> <label for="txtMobile">Mobile No. <span class="rightfloat"></span><span class="clear"></span></label> <input name="txtMobile" type="text" size="25" id="txtMobile" maxlength="10" class="form-control" value="<?php echo $row->mobilenumber;?>" /> </div> <div class="form-group"> <label for="txtcity">City Name <span class="style1">*</span></label> <input name="txtcity" type="text" size="25" id="txtcity" maxlength="10" class="form-control" value="<?php echo $row->city;?>" /> <span style="color:red"><?php echo form_error('txtcity');?></span> <span id="cityError" class="red"></span> </div> <button type="submit" class="btn btn-default" name="btnRegister">Submit</button> </form><?php } ?> </div> </div>
Now we check controller function how we use form validations in codeigniter, we need to call form_validation library in the constructor, which helps to use in all functions in the controller class.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?> <?php class User extends CI_Controller { public $data; public function __construct() { //Core controller constructor parent::__construct(); $this->load->model('user/user_service'); $this->load->library('form_validation'); $this->load->library("pagination"); } public function edit_user_profile() { $user_id=$this->session->userdata('user_id'); $name=$this->input->post('txtName', TRUE); //$email=$this->input->post('txtEmail', TRUE); $companyname=$this->input->post('txtcompanyname',TRUE); $Mobilenumber=$this->input->post('txtMobile',TRUE); $city=$this->input->post('txtcity',TRUE); $this->form_validation->set_rules('txtcity', 'city', 'required|xss_clean|trim'); if ($this->form_validation->run() == FALSE) { $data['query']=$this->user_service->get_user_details(); $this->settemplate->usertemp('user/edituser_profile',$data); } else { $update_password=array('firstname'=>$name,'company_name'=>$companyname,'mobilenumber'=>$Mobilenumber,'city'=>$city); $this->db->where('user_id',$user_id); $this->db->update('autoflip_user',$update_password); $this->session->set_flashdata('message','Your Profile Updated Successfully'); redirect('user/display_user_profile'); } } }
If you are interested in creating new wordpress blog, I prefer Dreamhost web hosting services, which offers best shared servers for high traffic blogs and easy to maintain Cpanel admin dashboard, without tech knowledge you can maintain your website using Cpanel. Dreamhost offers best low price plans for your websites. Especially eknowledgetree readers can avail 47 dollars discount for one year hosting plan. To avail this offer click below link.
If you are looking to maintain large websites, I prefer Bluehost web hosting services, which provides best support for your websites. Bluehost provides best security to your websites when compared to other hosting companies.
Thanks for reading this article.
Leave A Comment