1

i have a code from tutorial that doesn't work well. I don't know why, i am a beginner in codeigniter. I want to submit the button update, if duplicate a text will response below the textbox that the email is exist and also check it if the email is a valid email syntax.

Here is my html code :

<form id="reg_form" class="form-horizontal">
    <div class="form-group">
    <label class="control-label col-sm-3" for="curEmail">New Email</label>
    <div class="col-sm-6">
    <input type="text" class="form-control" name="email" id="email" placeholder="[email protected]" required data-fv-emailaddress-message="The value is not a valid email address">                                                           
    </div>
    <div class="col-sm-8 ajax_response_result"></div>                                                       
    </div>
    <button onclick="editEmail()" class="btn btn-success btn-sm "><i class="fa fa-refresh"></i> Update Profile</button>
</form>

Here is my ajax:

function editEmail() {

    jQuery.ajax({
    type: "POST",
    url: "<?php echo site_url('manager/profile/check_email') ?>",    
    data: $("#reg_form").serialize(),
    success: function(res) {
     $(".ajax_response_result").html(res);

     }
    });

}

My CodeIgniter Controller:

public function check_email() {
         $this->form_validation->set_rules('email', 'Email', 'required|is_unique[user.user_email]|valid_email');

         $this->form_validation->set_message('is_unique', 'The %s is already taken');
         if ($this->form_validation->run() == FALSE):
                echo 'Enter valid email.';          
        else :           
            unset($_POST);
            echo 'Available';
        endif;

  }
10
  • thank you for your answer sir. I think for unique it is from database. if it is exist. but it doesnt work well. Commented Oct 7, 2016 at 9:19
  • did u load $this->load->library('form_validation'); ? Commented Oct 7, 2016 at 9:20
  • yes sir. i already did. there are no errors sir. but the code behaves nothing. Commented Oct 7, 2016 at 9:22
  • already loaded. URL,form_validation,etc. Commented Oct 7, 2016 at 9:23
  • 1
    yes, after this success: function(res) { $(".ajax_response_result").html(res); } }); add return false; Commented Oct 7, 2016 at 9:32

1 Answer 1

2

There are several issue in your code, Revised & given below

HTML (method POST was not there in your code, editEmail here event missing etc etc)

<form id="reg_form" class="form-horizontal" method="POST">
    <div class="form-group">
    <label class="control-label col-sm-3" for="curEmail">New Email</label>
    <div class="col-sm-6">
    <input type="text" class="form-control" name="email" id="email" placeholder="[email protected]" required data-fv-emailaddress-message="The value is not a valid email address">                                                           
    </div>
    <div class="col-sm-8 ajax_response_result"></div>                                                       
    </div>
    <button onclick="editEmail(event)" class="btn btn-success btn-sm "><i class="fa fa-refresh"></i> Update Profile</button>
</form>

Now JS (e.preventDefault(); used for prevent form submitting etc etc)

function editEmail(e) {
    e.preventDefault();
    jQuery.ajax({
    type: "POST",
    url: "<?php echo site_url('manager/profile/check_email') ?>",    
    data: $("#reg_form").serialize(),
    success: function(res) {
     $(".ajax_response_result").html(res);

     }
    });

}
Sign up to request clarification or add additional context in comments.

5 Comments

@hey sir. very great answer sir. thanks for editting my code. it helps me very well. thank you very much sir.
@JcJohn enjoy your day
hello sir. may i ask again ? why is my error message from $this->form_validation->set_message('is_unique', 'The %s is already taken'); is not working ?
to get validation error all you have to do is use echo strip_tags(validation_errors()) instead echo 'Enter valid email.'; in controller
@RejoanulAlam sir, i mean the set_message error for every rules of form_validation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.