Forms provide a way for users to interact with the application and submit data.
HTML is easy to understand and write, but CodeIgniter makes things even easier. CodeIgniter has built-in functions to build HTML forms.
$this->load->helper('form');The following table shows the CodeIgniter functions for HTML tags:
| No. | HTML | CodeIgniter |
|---|---|---|
| 1 | | |
| 2 | | |
| 3 | | |
| 4 | | |
| 5 | | |
| 6 | | |
| 7 | | |
| 8 | | |
| 9 | | |
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form in CodeIgniter</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js">
</script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js">
</script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-12">
<?php
$attr = 'method="post"';
echo form_open('contacts/add', $attr);
?>
<div class="form-group">
<label for="name">Name:</label>
<?php echo form_input(["type"=>"text",
"name"=>"name",
"id"=>"name",
"class"=>"form-control"]); ?>
</div>
<div class="form-group">
<label for="email">Email:</label>
<?php echo form_input(["type"=>"email",
"name"=>"email",
"id"=>"email",
"class"=>"form-control"]); ?>
</div>
<div class="form-group">
<label for="country">Country:</label>
<?php
$country = ["India"=>"India", "US"=>"USA"];
echo form_dropdown(["name"=>"country",
"id"=>"country",
"options"=>$country,
"class"=>"form-control"]);
?>
</div>
<div class="form-group">
<?php echo form_button(["type"=>"submit",
"name"=>"submit"
,
"id"=>"submit", "class"=>"btn btn-primary",
"content"=>"Submit"]); ?>
</div>
</form>
</div>
</div>
</div>
</body>
</html>