Skip to main content

How to Validate Select in CodeIgniter


Installing CodeIgniter

If you don't have CodeIgniter installed yet, you can use this link to download the latest version of CodeIgniter. Note: I'm using version 3.1.7 in this tutorial.

After downloading, extract the file in the folder of your server. Since I'm using XAMPP as my localhost server, I've put the folder in htdocs folder of my XAMPP.

Then, you can test whether you have successfully installed codeigniter by typing your app name in your browser. In my case, I named my app as ci_select so I'm using the below code.

  1. localhost/ci_select

codeigniter successful download

Removing index.php in our URLs

By default, index.php is included in every URL of our app. To remove this, please refer to my tutorial How to Remove Index.Php in the URL of Codeigniter Application.

Creating our Controller

Next step is to create our controller. This will be in charge of our app.

Create a file named Members.php in application/controllers folder of our app and put the ff codes.

  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Members extends CI_Controller {
  5. public function __construct(){
  6. parent::__construct();
  7. $this->load->helper(array('url', 'form'));
  8. $this->load->library(array('session', 'form_validation'));
  9. }
  10. public function index(){
  11. $this->load->view('myform');
  12. }
  13. public function submit(){
  14. $this->form_validation->set_rules('member', 'Member', 'required');
  15. if ($this->form_validation->run() === TRUE){
  16. $this->session->set_flashdata('success', $this->input->post('member'));
  17. redirect('/');
  18. }
  19. else{
  20. $this->load->view('myform');
  21. }
  22. }
  23.  
  24. }

Creating our Routes

Next, we are going to set up the default route of our application.

Open routes.php located in application/config folder and find $route['default_controller'] and update its value by the code below.

  1. $route['default_controller'] = 'members';

Creating our Views

Lastly, we create the views of our app. Take note that I've use Bootstrap in the views which is included in the downloadables of this tutorial. But if you want, you may download bootstrap using this link.

Create this file inside application/views folder.

myform.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>How to Validate Select in CodeIgniter</title>
  6. <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1 class="page-header text-center">Validate Select in CodeIgniter</h1>
  11. <div class="row">
  12. <div class="col-sm-4 col-sm-offset-4">
  13. <?php
  14. if(validation_errors()){
  15. echo "
  16. <div class='alert alert-danger'>
  17. <h4>Error!</h4>
  18. <p>".validation_errors()."</p>
  19. </div>
  20. ";
  21.  
  22. }
  23.  
  24. if($this->session->flashdata('success')){
  25. echo "
  26. <div class='alert alert-success'>
  27. <h4>Success!</h4>
  28. <p>You selected: ".$this->session->flashdata('success')."</p>
  29. </div>
  30. ";
  31.  
  32. }
  33. ?>
  34. <form method="POST" action="<?php echo base_url(); ?>members/submit">
  35. <div class="form-group">
  36. <select class="form-control" name="member" id="member">
  37. <option value="" selected="selected">Select Member</option>
  38. <option value="Neovic" <?php echo set_select('member', 'Neovic'); ?>>Neovic</option>
  39. <option value="Gemalyn" <?php echo set_select('member', 'Gemalyn'); ?>>Gemalyn</option>
  40. <option value="Julyn" <?php echo set_select('member', 'Julyn'); ?>>Julyn</option>
  41. </select>
  42. </div>
  43. <button type="submit" class="btn btn-primary">Submit</button>
  44. </form>
  45. </div>
  46. </div>
  47. </div>
  48. </body>
  49. </html>

That ends this tutorial. Happy Coding :)

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.