• We just launched and are currently in beta. Join us as we build and grow the community.

CodeIgniter Ajax Signup with Validation using jQuery

RestInHeaven

System Reliability Specialist
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
98
Likes
30
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Installing CodeIgniter

If you don't have CodeIgniter installed yet, you can use this link to download the latest version of CodeIgniter which is 3.1.7 that I've used 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 codeigniter_register_ajax so I'm using the below code.

  1. localhost/codeigniter_register_ajax

codeginiter_welcome_5.png

Creating our Database

First we are going to create our database.

1. Open your phpMyAdmin.
2. Create a new database named codeigniter.
3. Click the database that we created, click SQL tab then paste the below code or import the included .sql file in the downloadable of this source code located in db folder.

  1. CREATE

    TABLE

    `users`

    (
  2. `id`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `email`

    varchar

    (

    40

    )

    NOT

    NULL

    ,
  4. `password`

    varchar

    (

    40

    )

    NOT

    NULL

    ,
  5. `fname`

    varchar

    (

    100

    )

    NOT

    NULL

    ,
  6. PRIMARY KEY

    (

    `id`

    )
  7. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

Connecting our App into our Database

Next, we're going to connect our codeigniter application to the database that we created earlier.

1. In your codeigniter app folder, open database.php located in application/config folder.

2. Update database.php with your credential the same as what I did in the image below.

database is where we define the database that we created earlier.

database_config_3.png

Configuring our Base URL

Next, we configure our base url to tell codeigniter that this is the URL of our site/application. We're gonna be using this a lot that's why we need to configure this.

1. In your codeigniter app folder, open config.php located in application/config folder.
2. Find and edit the ff line:

  1. $config['base_url'] = 'http://localhost/codeigniter_register_ajax';

where codeigniter_register_ajax is the name of your app folder.

Creating our Model

Next, we create the model for our app. Take note that the first letter of your model name should be in CAPITAL letter and the name of the model should be the same as the file name to avoid confusion.

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

  1. <?php
  2. class

    Users_model extends

    CI_Model {
  3. function

    __construct(

    )

    {
  4. parent::

    __construct(

    )

    ;
  5. $this

    ->

    load

    ->

    database

    (

    )

    ;
  6. }

  7. public

    function

    getAllUsers(

    )

    {
  8. $query

    =

    $this

    ->

    db

    ->

    get

    (

    'users'

    )

    ;
  9. return

    $query

    ->

    result

    (

    )

    ;
  10. }

  11. public

    function

    register(

    $user

    )

    {
  12. return

    $this

    ->

    db

    ->

    insert

    (

    'users'

    ,

    $user

    )

    ;
  13. }

  14. }
  15. ?>

Creating our Controller

Next step is to create our controller. Controllers follow the same naming convention as models.

Create a file named User.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. class

    User extends

    CI_Controller {

  4. function

    __construct(

    )

    {
  5. parent::

    __construct(

    )

    ;
  6. $this

    ->

    load

    ->

    model

    (

    'users_model'

    )

    ;
  7. // load form and url helpers
  8. $this

    ->

    load

    ->

    helper

    (

    array

    (

    'form'

    ,

    'url'

    )

    )

    ;
  9. // load form_validation library
  10. $this

    ->

    load

    ->

    library

    (

    'form_validation'

    )

    ;
  11. }

  12. public

    function

    index(

    )

    {
  13. $this

    ->

    load

    ->

    view

    (

    'register_form'

    )

    ;
  14. }

  15. public

    function

    fetch(

    )

    {
  16. $data

    =

    $this

    ->

    users_model

    ->

    getAllUsers

    (

    )

    ;
  17. foreach

    (

    $data

    as

    $row

    )

    {
  18. ?>
  19. <tr>
  20. <td><?php

    echo

    $row

    ->

    id

    ;

    ?>

    </td>
  21. <td><?php

    echo

    $row

    ->

    email

    ;

    ?>

    </td>
  22. <td><?php

    echo

    $row

    ->

    password

    ;

    ?>

    </td>
  23. <td><?php

    echo

    $row

    ->

    fname

    ;

    ?>

    </td>
  24. </tr>
  25. <?php
  26. }
  27. }

  28. public

    function

    register(

    )

    {
  29. $output

    =

    array

    (

    'error'

    =>

    false

    )

    ;

  30. /* Set validation rule for name field in the form */
  31. $this

    ->

    form_validation

    ->

    set_rules

    (

    'email'

    ,

    'Email'

    ,

    'valid_email|required'

    )

    ;
  32. $this

    ->

    form_validation

    ->

    set_rules

    (

    'password'

    ,

    'Password'

    ,

    'required|min_length[7]|max_length[30]'

    )

    ;
  33. $this

    ->

    form_validation

    ->

    set_rules

    (

    'fname'

    ,

    'Full Name'

    ,

    'required'

    )

    ;

  34. if

    (

    $this

    ->

    form_validation

    ->

    run

    (

    )

    ==

    FALSE

    )

    {
  35. $output

    [

    'error'

    ]

    =

    true

    ;
  36. $output

    [

    'message'

    ]

    =

    validation_errors(

    )

    ;
  37. }
  38. else

    {
  39. $user

    [

    'email'

    ]

    =

    $_POST

    [

    'email'

    ]

    ;
  40. $user

    [

    'password'

    ]

    =

    $_POST

    [

    'password'

    ]

    ;
  41. $user

    [

    'fname'

    ]

    =

    $_POST

    [

    'fname'

    ]

    ;

  42. $query

    =

    $this

    ->

    users_model

    ->

    register

    (

    $user

    )

    ;

  43. if

    (

    $query

    )

    {
  44. $output

    [

    'message'

    ]

    =

    'User registered successfully'

    ;
  45. }
  46. else

    {
  47. $output

    [

    'error'

    ]

    =

    true

    ;
  48. $output

    [

    'message'

    ]

    =

    'User registered successfully'

    ;
  49. }
  50. }

  51. echo

    json_encode

    (

    $output

    )

    ;
  52. }

  53. }

Defining our Default Controller

Next, we are going to set our default controller so that if no controller is defined, this default controller will be used.

Open routes.php located in application/config folder and set the default route to our user controller.

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

Creating our Views

Lastly, we create the views of our app.

Create the ff files inside application/views folder.

register_form.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CodeIgniter Ajax Signup with Validation</title>
  6. <link rel="stylesheet" type="text/css" href="<?php

    echo

    base_url(

    )

    ;

    ?>

    bootstrap/css/bootstrap.min.css">
  7. <script src="<?php

    echo

    base_url(

    )

    ;

    ?>

    jquery/jquery.min.js"></script>
  8. <script src="<?php

    echo

    base_url(

    )

    ;

    ?>

    bootstrap/js/bootstrap.min.js"></script>
  9. </head>
  10. <body>
  11. <div class="container">
  12. <h1 class="page-header text-center">CodeIgniter Ajax Signup with Validation</h1>
  13. <div class="row">
  14. <div class="col-sm-4">
  15. <div class="login-panel panel panel-primary">
  16. <div class="panel-heading">
  17. <h3 class="panel-title"><span class="glyphicon glyphicon-user"></span> Register
  18. </h3>
  19. </div>
  20. <div class="panel-body">
  21. <form id="regForm">
  22. <fieldset>
  23. <div class="form-group">
  24. <input class="form-control" placeholder="Email" type="text" name="email">
  25. </div>
  26. <div class="form-group">
  27. <input class="form-control" placeholder="Password" type="password" name="password">
  28. </div>
  29. <div class="form-group">
  30. <input class="form-control" placeholder="Full Name" type="text" name="fname">
  31. </div>
  32. <button type="submit" class="btn btn-lg btn-primary btn-block">Sign Up</button>
  33. </fieldset>
  34. </form>
  35. </div>
  36. </div>
  37. <div id="responseDiv" class="alert text-center" style="margin-top:20px; display:none;">
  38. <button type="button" class="close" id="clearMsg"><span aria-hidden="true">&times;</span></button>
  39. <span id="message"></span>
  40. </div>
  41. </div>
  42. <div class="col-sm-8">
  43. <table class="table table-bordered table-striped">
  44. <thead>
  45. <tr>
  46. <th>ID</th>
  47. <th>Email</th>
  48. <th>Password</th>
  49. <th>FullName</th>
  50. </tr>
  51. </thead>
  52. <tbody id="tbody">
  53. </tbody>
  54. </table>
  55. </div>
  56. </div>
  57. </div>
  58. <script type="text/javascript">
  59. $(document).ready(function(){
  60. getTable();

  61. $('#regForm').submit(function(e){
  62. e.preventDefault();
  63. var url = '<?php

    echo

    base_url(

    )

    ;

    ?>

    ';
  64. var reg = $('#regForm').serialize();
  65. $.ajax({
  66. type: 'POST',
  67. data: reg,
  68. dataType: 'json',
  69. url: url + 'index.php/user/register',
  70. success:function(response){
  71. $('#message').html(response.message);
  72. if(response.error){
  73. $('#responseDiv').removeClass('alert-success').addClass('alert-danger').show();
  74. }
  75. else{
  76. $('#responseDiv').removeClass('alert-danger').addClass('alert-success').show();
  77. $('#regForm')[0].reset();
  78. getTable();
  79. }
  80. }
  81. });
  82. });

  83. $(document).on('click', '#clearMsg', function(){
  84. $('#responseDiv').hide();
  85. });

  86. });
  87. function getTable(){
  88. var url = '<?php

    echo

    base_url(

    )

    ;

    ?>

    ';
  89. $.ajax({
  90. type: 'POST',
  91. url: url + 'index.php/user/fetch',
  92. success:function(response){
  93. $('#tbody').html(response);
  94. }
  95. });
  96. }
  97. </script>
  98. </body>
  99. </html>

That ends this tutorial. Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,270

322,965

322,974

Top