php - Check if existing username from controller and redirect to view page if already exists -
i have following username lookup function checks if username available.
<script language="javascript"> function lookupusername(){ var name = document.getelementbyid('username').value; //alert(name); $.post( 'users/ajax_lookupusername', { username: name }, function(response) { if (response == 1) { // alert('username available'); } else { // settimeout("alert("here am!")", 5000); alert('username taken! please select name.'); } }); } </script>
and here ajax_lookupusername function in controller:
function ajax_lookupusername(){ $username = $this->input->post('username'); $this->db->where('username', $username); $query = $this->db->get('mcb_users'); if ($query->num_rows() > 0){ echo 0; } else { echo 1; }
the code works fine. when user enters existing username, displays alert message, but, when user continues without modifying , clicks on save button, gets saved!
is there way redirect user before saving database controller?
you can using onsubmit
form validation also. but, there approach too. can through controller also. add following code in save function of controller, before inserting data database:
$userarraydb = $this->mdl_users->getallusername(); foreach($userarraydb $dbusersname) { if(($dbusersname->text == $username) && ($dbusersname->id != $user_id) ) { //echo $this->lang->line('user_name_must_be_unique'); //sleep(1); ?> <script> alert('record can not saved! username must unique.'); </script> <?php return false; } }
and add following function user model:
function getallusername() { $teller_id = $this->session->userdata('user_id'); $this->db->select('username text, id id'); $this->db->from('mcb_users'); $result = $this->db->get(); $username = $result->result(); //all user name taneko cha return $username; // return $result->result(); }
Comments
Post a Comment