Saturday, February 28, 2015

how to get and display post data codeigniter

first Setup codeigniter project Click Here

here you find how to get post data in controller and sent back to view.
Create Controller postdata.php in controller folder put the below code inside the file.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Postdata extends CI_Controller {

//constructor
function __construct()
{
parent::__construct();
$this->load->helper('url');
}
//index() method
public function index()
{
$this->load->view('postData');
}
//view() method
}
 ?>


Create view postData.php in view folder put the below code inside the file.
here we creating a form for getting basic info about user. form action(url where you get post data) and method(post) need to specify.


<html>
<head><title>Post Data Example</title></head>
<style type="text/css">
.title { font-family: Arial, Helvetica, sans-serif; text-align:center;width:350px; }
.tableDesign { font-size:16px;font-family:Arial, Helvetica, sans-serif; width:350px; height: 270px; }
table { background-color:#CCC; }
table input { height:25px;font-size:16px; }
table tr { height:2px; }
.errorMessage { color:red;text-align:center; }</style>
<body>
   <form id="form1" name="form1" method="post" action="<?php echo base_url(); ?>index.php/postdata/view">
      <label><div class="title"><u>Post Data Example</u></div></label>
      <table style="" class="tableDesign">
         <tr><td colspan="2"><?php if(isset($_GET['err_message'])){ echo "<div class='errorMessage'>".$_GET['err_message'].'</div>'; } ?></td></tr>
         <tr><td>First Name: </td><td><input type="text" name="txtFirstName" /></td></tr>
         <tr><td>Last Name: </td><td><input type="text" name="txtLastName" /></td></tr>
         <tr><td>Gender:</td><td><input type="radio" name="txtGender" value="Male" checked />
            Male&nbsp;<input type="radio" name="txtGender" value="Female" />Female</td></tr>
         <tr><td>Mobile No.: </td><td><input type="text" name="txtMobileNo" /></td></tr>
         <tr><td>E-mail: </td><td><input type="text" name="txtEmail" /></td></tr>
         <tr><td>&nbsp;</td><td><input type="submit" name="submit" value="Submit" /></td></tr>
    </table>
</form>
</body>
</html>


Now, Open your browser go through below url

http://localhost/codeigniter/index.php/postdata

here you can see html form input fields to collect basic info.

Put below code inside  Controller postdata.php after index method(after //view() method )
Method get the post data and sent back to view for display data.

         public function view()
{
/* $data['firstName'] = $this->input->post('txtFirstName');
$data['lastName'] = $this->input->post('txtLastName');
$data['gender'] = $this->input->post('txtGender');
$data['mobileNo'] = $this->input->post('txtMobileNo');
$data['email'] = $this->input->post('txtEmail'); */

$data['postData'] = $this->input->post();
$this->load->view('postDataDisplay', $data);
}


Create view postDataDisplay.php in view folder put the below code inside the file for displaying post data result on the browser.

<html>
<head>
<title>Post Data Example</title>
</head>
<style type="text/css">
.title { font-family: Arial, Helvetica, sans-serif; text-align:center;width:350px; }
.tableDesign { font-size:16px;font-family:Arial, Helvetica, sans-serif; width:350px; height: 210px; }
table { background-color:#CCC; }
table input { height:25px;font-size:16px; }
table tr { height:2px; }
.errorMessage { color:red;text-align:center; }</style>
<body>
  <label><div class="title"><u>Post Data Display</u></div></label>
  <table style="" class="tableDesign">

  <!--<tr><td>First Name: <?php echo $firstName; ?> </td></tr>-->

    <tr><td>First Name: <strong><?php echo $postData['txtFirstName']; ?></strong></td></tr>
    <tr><td>Last Name:  <strong><?php echo $postData['txtLastName']; ?></strong></td></tr>
    <tr><td>Gender: <strong><?php echo $postData['txtGender']; ?></strong></td></tr>
    <tr><td>Mobile No.:  <strong><?php echo $postData['txtMobileNo']; ?></strong></td></tr>
    <tr><td>E-mail:  <strong><?php echo $postData['txtEmail']; ?></strong></td></tr>
  </table>
</form>
</body>
</html>


Thanks for the visiting this blog.

hello world codeigniter example

hello friends, here you can find source code for,  how to create simple hello world application in codeigniter

first download source for codeigniter from here  Download Codeigniter  extract zip file in www folder rename default name with your project name.

change your configuration in config file application/config.php  config.php file

$config['base_url'] = '';
             to
$config['base_url'] = 'http://localhost/codeigniter/'; //change your project configuration here.

create controller first.php in controller folder. Copy and Past below code in that file
controller/first.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class First extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('first'); //load view file first.php no need for extention  .php it added by default
}
}
?>

Create view first.php in view folder. Copy and Past below code in  that file.
view/first.php
<html>
<head><title>First Example</title></head>
<style type="text/css">
.design{ background: #CCCCCC;width: 20%;text-align: center;margin-left: 40%; }
</style>
<body>
<div class="design"><h1>Hello World.!<Br>Welcome Done...!!!</h1></div>
</body>
</html>

Now open your browser and go through below link.

http://localhost/codeigniter/index.php/first

- codeigniter is your project name .

- need to add index.php in url. It is also possible to remove index.php from url creating .htaccess file in project root folder using url routing.

- first is your controller name.

- Now you done you can see the output on browser. Thank you for visiting my blog.