1
votes

I have an edit model, contains 2 forms, name and e-mail, I put some javascript in my form, so when user edit their name/email, program will check whether the name/email already in database or not, if already in database there will be alert that show the name/email already exist.

The problem is, for name form that code is work perfectly but when I tried it in email form, it's only read one if statement which is will say error email already exist even though the email not exist in database.

One important things is that URL in codeigniter can't read symbols like @, so the same code for name verification cannot be use in email verification, so i made a new function and all my related code just like this:

Model

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

Controller

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

Javascript

var check5; 
$("#editemail").bind("keyup change", function(){
    var editemail = $(this).val();
    $.ajax({
        url:'kasir/cekEmailEdit2',
        type:"POST",
        data:{send:true, email:email},
        success:function(data){ alert(email);
            if (editemail!=oldEmail){
                if(data==true){
                    //email not exist in database, so form can be edit
                    $("#report5").text("berhasil karna nama email baru");
                    check5=1; 
                }else{
                    //email exist in database, show alert
                    $("#report5").text("*email sudah terpakai"); 
                    check5=0; 
                }
            }else{
                //success submit form because no data has been changed
                $("#report5").text("berhasil karna gajadi ganti email");
                check5=1; 
            }
        }
    });
});
1
Try to enter email which is already in database and see result. and change your controller. use true instead of false and vice-versa.Jaydeep

1 Answers

1
votes

Try this:

function email_exists($email)
{
    return $this->db->where('email', $email)->count_all_results('petugas');
}

public function register_email_exists()
{
    $result = array(
        'status' => 1 // denotes exists
    );

    if($this->input->post('email') != ''){
        $email = $this->input->post('email');
        if($this->Crud->email_exists($email)){
            $result['status'] = 1;
        }else{
            $result['status'] = 0;
        }
    }

    echo header('Content-Type: application/json');
    echo json_encode($result);
}

The JS part:

success:function(data){ 
    data = JSON.parse(data);
    if(data.status){
        alert('Exists');
    }else{
        alert('Not exists');
    }
}