Configure Your Codeigniter Application
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 <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> </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. In this example whole post data sent, also possible to get single request and displayed on view.
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> single field data displayed -->
<!-- display post data result -->
<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>
Now you done try submitting form data and see the output.