How to check if email exist while typing in MYSQL using jQuery, AJAX and CodeIgniter

anonymous369

I want to validate my form's input with database, so when user type on form's input and contain email already in use or exists it will display an alert and cant submit. I use CodeIgniter framework and jQuery.

I've tried using the code below to check if name exists and this could work. But when I apply it to the other case for email, it doesn't work and display message "The URI you submitted has disallowed characters."

How is the correct way to fix this?

View (kasir_halaman.php) :

<div id="addModal" class="modal fade" role="modal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h3 class="modal-title"><span class="glyphicon glyphicon-plus"></span> Tambah Kasir</h3>
                </div>
                <div class="modal-body">
                    <form action="<?php echo site_url('admin/kasir/addpetugas'); ?>" method="post" enctype="multipart/form-data">
                        <div class="form-group">
                            <label>Nama</label>
                            <input type="text" id="nama" name="nama" class="form-control" maxlength="100" required>
                        </div>
                        <div class="form-group">
                            <label>E-mail</label>
                            <input type="email" id="email" name="email" class="form-control" maxlength="150" required>
                        </div>
                        <div class="form-group">
                            <label>Kategori</label>
                            <select class="form-control" name="kategoripetugas" required>
                                <option value=""> -- Pilih Kategori -- </option>
                                <option value="1">Admin</option>
                                <option value="2">Kasir</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>Password</label>
                            <input type="password" name="password" class="form-control" maxlength="30">
                        </div>
                        <div class="form-group">
                            <label>Ulangi Password</label>
                            <input type="password" name="confirmpassword" class="form-control" maxlength="30">
                        </div>
                        <button type="submit" class="btn btn-primary" style="width:100%;">Tambah</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

Controller (kasir.php) :

public function cekData($table, $field, $data)
{
    $match = $this->Crud->read($table, array($field=>$data), null, null);
    if($match->num_rows() > 0){
        $report = 2;
    }else{
        $report = 1;
    }
    echo $report;
}

public function register_email_exists()
{
    if (array_key_exists('email',$_POST)) {
        if ($this->Crud->email_exists($this->input->post('email')) == TRUE ) {
            echo false;
        } else {
            echo true;
        }
    }
}

Model (Crud.php) :

function email_exists($email)
{
    $this->db->where('email', $email);
    $query = $this->db->get('petugas');
    if( $query->num_rows() > 0 ){ return TRUE; } else { return FALSE; }
}

jQuery AJAX (petugas.js) :

$(document).ready(function(){
var check1=0; var id;
$("#nama").bind("keyup change", function(){
    var nama = $(this).val();
    $.ajax({
        url:'kasir/cekData/petugas/nama/'+nama,
        data:{send:true},
        success:function(data){
            if(data==1){
                $("#report1").text("");
                check1=1;
            }else{
                $("#report1").text("*nama petugas sudah terpakai");
                check1=0;
            }
        }
    });
});

var check2=0;
$("#email").bind("keyup change", function(){
    //var email = $(this).val();
    $.ajax({
        url:'kasir/register_email_exists',
        data:{send:true},
        success:function(data){
            if(data==1){
                $("#report2").text("");
                check2=1;
            }else{
                $("#report2").text("*email sudah terpakai");
                check2=0;
            }
        }
    });
});

var check4=0;
$("#confirmpassword").bind("keyup change", function(){
    var password = $("#password").val();
    var confirmpassword = $(this).val();

    if (password == confirmpassword){
        $("#report4").text("");
        check4=1;
    }else{
        $("#report4").text("*Password tidak sama");
        check4=0;
    }
});

$("#submit").click(function(event){
    if(check1==0){
        event.preventDefault();
    }
    if(check4==0){
        event.preventDefault();
    }
});

});
Armen

Use ajax post method instead and take data at php side from POST request

you can check more about jquery ajax here: http://api.jquery.com/jquery.post/

and about php post here: http://php.net/manual/en/reserved.variables.post.php

//JS
$("#email").bind("keyup change", function(){

  var email = $(this).val();

  $.ajax({
    url:'kasir/register_email_exists',
    type: "POST",// <---- ADD this to mention that your ajax is post
    data:{ send:true, email:email },// <-- ADD email here as pram to be submitted
    success:function(data){
        if(data==1){
            $("#report2").text("");
            check2=1;
        }else{
            $("#report2").text("*email sudah terpakai");
            check2=0;
        }
    }
  });

});

// PHP
// At php side take your data  from $_POST
$send = $_POST['send'];
$email = $_POST['email'];
...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

check if email exist in database SQL using codeigniter and AJAX

From Dev

how to check username and email already exist except for one particular id in database using jquery validator

From Dev

How to check if email already exist in angularjs using spring boot

From Dev

ajax Check if Email already exist Ruby on Rails

From Dev

how to Check ID verification if already exists in the database using PHP CodeIgniter, JQuery, AJAX

From Dev

error on ajax while using in codeigniter

From Dev

Using jQuery to check if a class exist

From Dev

how to check email exist in rails, devise gem

From Dev

How to check if there exist an input with specific name attr using jQuery

From Dev

How to check entered value is exist or not using Jquery or javascript

From Dev

Email validation using JQuery Ajax

From Dev

Email already exist with Jquery validator plugin in codeigniter is not working

From Dev

How to check if email already exists in db with CodeIgniter

From Dev

How to check email address exists or not in codeigniter

From Dev

How to check two values in array mysql using codeigniter

From Dev

Data check in database if already exist in codeigniter through ajax

From Dev

How to call a codeigniter helper function using JQuery AJAX?

From Dev

how to create like button using jquery, ajax and codeigniter

From Dev

How to redirecting page to another page using ajax jquery in codeigniter?

From Dev

Unexpected token < while using ajax and codeigniter

From Dev

check username is unique or not using ajax in codeigniter

From Dev

Check if username and password matched using ajax codeigniter

From Dev

Regex - how to check while typing (even if the text is not complete)

From Dev

Check if url param exist using jquery

From Dev

Using PHP Check Email ID Already Exist Or Not in Database

From Dev

Check is character is in array while typing

From Dev

Validating form input fields while typing using jQuery?

From Dev

To check whether the email exists in database using Jquery

From Dev

How to post mulitple check box value using ajax jquery in laravel

Related Related

  1. 1

    check if email exist in database SQL using codeigniter and AJAX

  2. 2

    how to check username and email already exist except for one particular id in database using jquery validator

  3. 3

    How to check if email already exist in angularjs using spring boot

  4. 4

    ajax Check if Email already exist Ruby on Rails

  5. 5

    how to Check ID verification if already exists in the database using PHP CodeIgniter, JQuery, AJAX

  6. 6

    error on ajax while using in codeigniter

  7. 7

    Using jQuery to check if a class exist

  8. 8

    how to check email exist in rails, devise gem

  9. 9

    How to check if there exist an input with specific name attr using jQuery

  10. 10

    How to check entered value is exist or not using Jquery or javascript

  11. 11

    Email validation using JQuery Ajax

  12. 12

    Email already exist with Jquery validator plugin in codeigniter is not working

  13. 13

    How to check if email already exists in db with CodeIgniter

  14. 14

    How to check email address exists or not in codeigniter

  15. 15

    How to check two values in array mysql using codeigniter

  16. 16

    Data check in database if already exist in codeigniter through ajax

  17. 17

    How to call a codeigniter helper function using JQuery AJAX?

  18. 18

    how to create like button using jquery, ajax and codeigniter

  19. 19

    How to redirecting page to another page using ajax jquery in codeigniter?

  20. 20

    Unexpected token < while using ajax and codeigniter

  21. 21

    check username is unique or not using ajax in codeigniter

  22. 22

    Check if username and password matched using ajax codeigniter

  23. 23

    Regex - how to check while typing (even if the text is not complete)

  24. 24

    Check if url param exist using jquery

  25. 25

    Using PHP Check Email ID Already Exist Or Not in Database

  26. 26

    Check is character is in array while typing

  27. 27

    Validating form input fields while typing using jQuery?

  28. 28

    To check whether the email exists in database using Jquery

  29. 29

    How to post mulitple check box value using ajax jquery in laravel

HotTag

Archive