0

How check if a username is in my database using ajax and code igniter? I managed to create function for no empty fields, and no invalid email. Now only function I could not complete is for username existence in DB.

Function: I want ajax to display me a message when some is trying to create username, and user name entered already exists in system.

function reg_new_user() {
  var frstname = document.getElementById('first_name').value;
  var lstname = document.getElementById('last_name').value;
  var uemail = document.getElementById('user_email').value;
  var uname = document.getElementById('user_name').value;
  var upasswd = document.getElementById('user_password').value;

  if (frstname == '' || lstname == '' || uemail == '' || uname == '' || upasswd == '') {
    alert('All fields are required');
    $('#add_new_user_mod').modal('hide');
    redirect('admin/Welcome/users');
  }
  if (uemail.indexOf('@') <= 0) {
    alert('Invalid Email address!');
    $("form").trigger("reset");
    redirect('admin/Welcome/users');
  }

  $.ajax({
    url: "<?php echo base_url().'index.php/admin/Welcome/add_user_new'; ?>",
    data: 'name=' + frstname + '&lastname=' + lstname + '&email=' + uemail + '&username=' + uname + '&password=' + upasswd,
    async: false,
    type: "POST",
    success: function(data) {
      $('#add_new_user_mod').modal('hide');
      alert('User added successfully');
      window.location = "<?php echo base_url().'index.php/admin/Welcome/users'; ?>";
    }
  });
  return false;
}
<div class="tools">
  <div class="btn-group">
    <div class="btn-group">


      <a class="btn  btn-info btn-raised ink-reaction" onclick="add_new_user_mod(); ">Add User</a>

    </div>
  </div>
</div>
<div class="modal fade" id="add_new_user_mod" tabindex="-1" role="dialog" aria-labelledby="simpleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="simpleModalLabel">Add Item</h4>
      </div>
      <div class="modal-body" id="adibody">

        <div id="err"></div>
        <form>
          <div class="form-group">
            <label>FirstName</label>
            <input type="text" class="form-control" id="first_name">
          </div>
          <div class="form-group">
            <label>LastName</label>
            <input type="text" class="form-control" id="last_name">
          </div>
          <div class="form-group">
            <label>Email</label>
            <input type="text" class="form-control" id="user_email">
          </div>
          <div class="form-group">
            <label>Username</label>
            <input type="text" class="form-control" id="user_name">
            <span id="feedback"></span>
          </div>
          <div class="form-group">
            <label>Password</label>
            <input type="password" class="form-control" id="user_password">
          </div>
          <div class="form-group">
            <button type="button" class="btn btn-success" onclick="reg_new_user();">Register</button>
          </div>
        </form>


      </div>
      <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
  </div>
  <!-- /.modal -->
  <!-- END SIMPLE MODAL MARKUP -->
</div>

7
  • You are mixing CI and JS Commented Apr 17, 2019 at 5:04
  • where is your code to check user name is already exists in controller and Model? second please use the proper format for data in Ajax Commented Apr 17, 2019 at 5:09
  • @DanishAli i don't see that anywhere... Commented Apr 17, 2019 at 5:18
  • @Alex I think this line of code redirect('admin/Welcome/users'); Commented Apr 17, 2019 at 5:22
  • he probably has a js function for this, otherwise it is indeed a mix Commented Apr 17, 2019 at 5:23

1 Answer 1

1

It is smart to do validation both on the front and backend, while I would argue against using alert() from a ux perspective.

Given that, I would implement form validation which would solve 2 problems: backend check (because client-side can never be trusted), and to make sure the user name is unique.

For example

function add_new_user() {
    $this->load->library('form_validation');
    $this->form_validation->set_rules('name', 'Name', 'required');
    // ^ do the same for your other fields
    $this->form_validation->set_rules('username', 'Username', 'required|is_unique[users.username]'); // is_unique `table_name`.`username field`
    if (!$this->form_validation->run()) {
        echo json_encode(['status' => 'error', 'msg' => validation_errors()]);
    }
    // post stuff to database
    echo json_encode(['status' => 'success', 'msg' => '']);
}

And js would look something like:

$.ajax({
    url: "<?php echo base_url() . 'index.php/admin/Welcome/add_user_new'; ?>",
    data: 'name=' + frstname + '&lastname=' + lstname + '&email=' + uemail + '&username=' + uname + '&password=' + upasswd,
    async: false,
    type: "POST",
    dataType: 'json',
    success: function (data) {
        if (data.status == 'success') {
            $('#add_new_user_mod').modal('hide');
            alert('User added successfully');
            window.location = "<?php echo base_url() . 'index.php/admin/Welcome/users'; ?>";
        } else {
            alert(data.msg);
        }
    }
});
Sign up to request clarification or add additional context in comments.

Comments

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.