-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeignitor dropdown 2.txt
105 lines (95 loc) · 2.77 KB
/
codeignitor dropdown 2.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
To create a dropdown in CodeIgniter, you can use its built-in form helper or manually generate HTML. Here’s an example of both methods:
1. Using CodeIgniter Form Helper
CodeIgniter's form helper simplifies the process of creating forms and their elements, including dropdowns.
Controller Code
php
Copy code
<?php
class DropdownController extends CI_Controller {
public function index() {
// Data for the dropdown
$data['options'] = [
'' => 'Select an option',
'1' => 'Option 1',
'2' => 'Option 2',
'3' => 'Option 3'
];
// Load the view
$this->load->view('dropdown_view', $data);
}
}
?>
View Code (dropdown_view.php)
php
Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dropdown Example</title>
</head>
<body>
<h1>Dropdown Example</h1>
<?php
// Open the form
echo form_open('dropdowncontroller/index');
// Dropdown
echo form_label('Choose an Option:', 'dropdown');
echo form_dropdown('dropdown', $options, '', ['id' => 'dropdown']);
// Submit button
echo form_submit('submit', 'Submit');
// Close the form
echo form_close();
?>
</body>
</html>
2. Using Standard HTML
If you prefer not to use CodeIgniter's form helper, you can create the dropdown manually.
Controller Code
php
Copy code
<?php
class DropdownController extends CI_Controller {
public function index() {
// Data for the dropdown
$data['options'] = [
'' => 'Select an option',
'1' => 'Option 1',
'2' => 'Option 2',
'3' => 'Option 3'
];
// Load the view
$this->load->view('dropdown_view', $data);
}
}
?>
View Code (dropdown_view.php)
php
Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dropdown Example</title>
</head>
<body>
<h1>Dropdown Example</h1>
<form action="dropdowncontroller/index" method="post">
<label for="dropdown">Choose an Option:</label>
<select name="dropdown" id="dropdown">
<?php foreach ($options as $key => $value): ?>
<option value="<?= $key; ?>"><?= $value; ?></option>
<?php endforeach; ?>
</select>
<button type="submit">Submit</button>
</form>
</body>
</html>
Key Notes
Form Helper:
Pros: Automatically integrates CSRF protection and error handling.
Cons: Requires loading the form helper.
Manual HTML:
Pros: More flexibility in customizing the HTML.
Cons: Manual handling of CSRF tokens and validation.
Let me know which approach suits your needs or if you'd like additional features like dynamic options via AJAX!