{"id":3966,"date":"2019-03-28T17:51:52","date_gmt":"2019-03-28T12:21:52","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=3966"},"modified":"2019-04-04T09:33:56","modified_gmt":"2019-04-04T04:03:56","slug":"validation-form-in-codeigniter-using-session-library","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/validation-form-in-codeigniter-using-session-library\/","title":{"rendered":"VALIDATION FORM IN CODEIGNITER USING SESSION LIBRARY"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>VALIDATION FORM IN CODEIGNITER USING SESSION LIBRARY<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Date :28\/03\/2019<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Introduction:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following tutorial implementing form validation using session library in codeigniter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First of all, we should create database and table. we can create database client  and we can create table form .<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To implement the form validation we  need to create four steps.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>step1:<\/strong>  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">     We have to create the form in view and this is considered as our default form.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">     In this form we are entering the data.Those data validated to the      following steps.<br> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">     we can save it as myform.php<strong>(application\/view\/myform.php).<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;html&gt;\n   &lt;head&gt;\n      &lt;title&gt;My Form&lt;\/title&gt;\n   &lt;\/head&gt;\n   &lt;body&gt;\n      &lt;!-- &lt;form action = \"\"&gt; --&gt;\n      &lt;?php echo form_open('Form\/index1'); ?&gt;  \n      &lt;h5&gt;firstname&lt;\/h5&gt;\n      &lt;input type = \"text\" name = \"firstname\" value=\"\"&gt;\n      &lt;h5&gt;gender&lt;\/h5&gt;\n      &lt;input type = \"text\" name = \"gender\" value=\"\"&gt;  \n      &lt;h5&gt;hobbies&lt;\/h5&gt;\n      &lt;input type = \"text\" name = \"hobbies\" value=\"\"&gt;\n      &lt;h5&gt;district&lt;\/h5&gt;\n      &lt;input type = \"text\" name = \"district\" value=\"\"&gt;\n      &lt;h5&gt;discription&lt;\/h5&gt;\n      &lt;input type = \"text\" name = \"discription\"  value=\"\"&gt;        \n      <div><\/div>\n      &lt;?php\n         echo form_close();\n         ?&gt;  \n   &lt;\/body&gt;\n&lt;\/html&gt;\n\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The above code Create the simple form.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>step2:<\/strong> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">     Then we create the controller page and check the validation in that    page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">     Above data validated in this controller part. we can save it as Form.php<strong>(application\/controller\/Form.php).<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n   class Form extends CI_Controller { \n   \n      public function index() { \n         \n         #Used to load the form library\n         $this-&gt;load-&gt;helper(array('form', 'url'));\n   \n         #Used to load the session library\n         $this-&gt;load-&gt;library(\"session\");\n   \n         #Used to load the view library\n         $this-&gt;load-&gt;view('myform');\n         \n      }\n   public function index1()\n   {\n      \n      $this-&gt;load-&gt;library(\"session\");\n   \n      #used to load the form validation library\n      #the form validation library used to load the set_rules functions\n      $this-&gt;load-&gt;library('form_validation');\n   \n      \n      #the following functions check wheather entered details are write or wrong\n      $this-&gt;form_validation-&gt;set_rules('firstname', 'firstname', 'required');\n      $this-&gt;form_validation-&gt;set_rules('gender', 'gender', 'required');\n      $this-&gt;form_validation-&gt;set_rules('hobbies', 'hobbies', 'required');\n      $this-&gt;form_validation-&gt;set_rules('district', 'district', 'required');\n      $this-&gt;form_validation-&gt;set_rules('discription', 'discription', 'required');\n   \n   #the above validation is fail it enter into the following if condition\n   if ($this-&gt;form_validation-&gt;run() == FALSE) \n   {    \n        $postData = array(\n                'firstname' =&gt; $this-&gt;input-&gt;post('firstname'),\n                'gender' =&gt; $this-&gt;input-&gt;post('gender'),\n                  'hobbies' =&gt; $this-&gt;input-&gt;post('hobbies'),\n                    'district' =&gt; $this-&gt;input-&gt;post('district'),\n                      'discription' =&gt; $this-&gt;input-&gt;post('discription')\n                 );   \n      \n       echo json_encode($this-&gt;form_validation-&gt;error_array());  \n      $this-&gt;session-&gt;set_userdata($postData);\n      $this-&gt;load-&gt;view('myform1');\n       \n   } \n   \n   #the validation part is pass the else part is executed\n   else \n   {\n   \n   $this-&gt;load-&gt;model('Insert_model');\n          $postData = array(\n                'firstname' =&gt; $this-&gt;input-&gt;post('firstname'),\n                'gender' =&gt; $this-&gt;input-&gt;post('gender'),\n                  'hobbies' =&gt; $this-&gt;input-&gt;post('hobbies'),\n                    'district' =&gt; $this-&gt;input-&gt;post('district'),\n                      'discription' =&gt; $this-&gt;input-&gt;post('discription')\n                 );  \n         print_r($insert = $this-&gt;Insert_model-&gt;insert($postData)); \n          echo \"Data inserted successfully\";\n       }\n         }\n   \n   \n       \n   }\n   ?&gt; <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The above code is validate our data.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">     * $this-&gt;load-&gt;helper(array('form', 'url')); = Used to load the form library.\n\n     * $this-&gt;load-&gt;library(\"session\"); = Used to load the session library.\n\n     * $this-&gt;load-&gt;library('form_validation'); = Used to load the form validation library. <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>step3:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> Again we create a another view page for reload the form.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> This form loaded when the given details is wrong and it shows error message we can save it as myform1.php<strong>(application\/view\/myform1.php).<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;html&gt;\n   &lt;head&gt;\n      &lt;title&gt;My Form&lt;\/title&gt;\n   &lt;\/head&gt;\n   &lt;body&gt;\n      &lt;!-- &lt;form action = \"\"&gt; --&gt;\n      &lt;?php echo form_open('Form\/index1'); ?&gt;  \n      &lt;h5&gt;firstname&lt;\/h5&gt;\n      &lt;input type = \"text\" name = \"firstname\" value=\"&lt;?php echo $this-&gt;session-&gt;userdata('firstname'); ?&gt;\"&gt;\n      &lt;h5&gt;gender&lt;\/h5&gt;\n      &lt;input type = \"text\" name = \"gender\" value=\"&lt;?php echo $this-&gt;session-&gt;userdata('gender'); ?&gt;\"&gt;  \n      &lt;h5&gt;hobbies&lt;\/h5&gt;\n      &lt;input type = \"text\" name = \"hobbies\" value=\"&lt;?php echo $this-&gt;session-&gt;userdata('hobbies'); ?&gt;\"&gt;\n      &lt;h5&gt;district&lt;\/h5&gt;\n      &lt;input type = \"text\" name = \"district\" value=\"&lt;?php echo $this-&gt;session-&gt;userdata('district'); ?&gt;\"&gt;\n      &lt;h5&gt;discription&lt;\/h5&gt;\n      &lt;input type = \"text\" name = \"discription\"  value=\"&lt;?php echo $this-&gt;session-&gt;userdata('discription'); ?&gt;\"&gt;        \n      <div><\/div>\n      &lt;?php\n         echo form_close();\n         ?&gt;  \n   &lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The above code create the another form.It contain our data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step4:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here we have to create the mode form our database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can save it as a Insertmodel.php <strong>(application\/model\/Insert_model.php).<\/strong> <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Insert_model extends CI_Model \n{\n\tpublic function insert($data = array()) \n\t{\n        #here form is our table name\n        $insert = $this-&gt;db-&gt;insert('form', $data);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The above code connect our form with database.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Finally The all above there steps completed form will displayed.<br> we can fill the form and click the submit.<\/li><li> If we missed any columns, It will display another form(myform1.php). The form containing our data along with error message.<\/li><li>Otherwise the data inserted into our database.Then it display success message like Data inserted successfully.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conclusion:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The process continue untill we submit correct data.if we enter the correct <br>data it inserted into our database.and show success message. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for using pheonixsolutions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You find this tutorial helpful? Share with your friends to keep it alive.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>VALIDATION FORM IN CODEIGNITER USING SESSION LIBRARY Date :28\/03\/2019 Introduction: The following tutorial implementing form validation using session library in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[341,599],"class_list":["post-3966","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-codeigniter","tag-form-validation","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-11Y","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=3966"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3966\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=3966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=3966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=3966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}