Welcome Guest, Not a member yet? Register   Sign In
Issue with JQuery remote validation or Codigniter syntax?
#1

[eluser]Dandy_andy[/eluser]
I have an issue with a JQuery remote validation in my Codeigniter project. I'm not sure if this is a problem with the JQuery side or Codeigniter.

I have two remote requests from a form, one calling to search for usernames that already exist and the other to call for emails that already exist. The username remote request works fine but for some reason the email one doesn't work at all (it did work up until a week ago and I can't remember what, if anything, I changed to stop it from working).

Bearing in mind the username request is working fine, am I missing something obvious for the email remote request???

The relevant part of my view is:-

Code:
<form method="post" action="<?php echo base_url('register/verify'); ?>" class="form-horizontal" id="registerform" name="registerform">
  
      <fieldset>
      
            <div class="control-group">      
            <label class="control-label" for="usernamenew"><strong>Select a username</strong></label>
         <div class="controls">
                &lt;input type="text" class="span3" id="usernamenew" name="usernamenew" placeholder="Enter unique username" /&gt;
                &lt;!---<div class="alert alert-error"></div>---&gt;
         </div>
            </div>
    
      <div class="control-group">  
            <label class="control-label" for="em"><strong>Your email</strong></label>
         <div class="controls">
             &lt;input type="text" class="span3" id="em" name="em" placeholder="Your email address" /&gt;
             &lt;!---<div class="alert alert-error"></div>---&gt;
         </div>
            </div>
                
            
         <div class="controls">
         &lt;input type="submit" class="btn-large btn-danger" value="Submit" /&gt;
   </div>

      </fieldset>
      &lt;/form&gt;

[removed]
  $(document).ready(function(){
   $("#registerform").submit(function() {
   return $("#registerform").valid();
   });
   $("#registerform").validate({
    rules:{
     usernamenew:{required:true, alphanumeric:true, rangelength:[4, 20], remote:{url:"&lt;?php echo base_url('register/check_username'); ?&gt;", type:"post", data:$("usernamenew").val()}},
     password:{required:true, minlength:6},
     confirm_password:{required:true, equalTo:"#password"},
     em:{required:true, email:true, remote:{url:"&lt;?php echo base_url('register/check_email'); ?&gt;", type:"post", data:$("em").val()}},
     verify:{required:true},
     terms:{required:true}
    },
    messages:{
     usernamenew:{rangelength:"Must be between 4 and 20 characters", remote:"Username already exists. Please choose another"},
     password:{required:"Please enter a password", minlength:"Password must have at least 6 characters"},
     confirm_password:{required:"Please confirm your password", equalTo:"Password and Confirm Password must match"},
     em:{required:"Enter your email address", remote:"That email is already in use. Please choose another"},
     verify:{required:"<---"},
     terms:{required:">"}
    },
    errorClass: "help-inline",
    errorElement: "span",
    highlight:function(element, errorClass, validClass) {$(element).parents('.control-group').addClass('error');},
    unhighlight:function(element, errorClass, validClass) {$(element).parents('.control-group').removeClass('error'); $(element).parents('.control-group').addClass('');}
   });
  });
[removed]

My Controller has the following functions:-

Code:
public function check_username() {

  $this->load->model('user_model');
  $usr = $this->input->post('usernamenew');
  $resultusername = $this->user_model->check_username_exist($usr);
  if($resultusername)
   {
   echo "false";
   }
   else
   {
   echo "true";
   }
}//function check_username




public function check_email() {

  $this->load->model('user_model');
  $em = $this->input->post('em');
  $result = $this->user_model->check_email_exist($em);
  if($result)
   {
   echo "false";
   }
   else
   {
   echo "true";
   }
}//function check_username

And my model uses:-

Code:
//check username address doesn't already exist during registration
public function check_username_exist($usr) {

  $this->db->where("username", $usr);
  $query = $this->db->get("members");
  
  $this->db->where("username", $usr);
  $query2 = $this->db->get("verification");
  
  if (($query->num_rows()>0) or ($query2->num_rows()>0))
   {
   return true;
   }
   else
   {
   return false;
   }
  
}


//check email address doesn't already exist during registration
public function check_email_exist($em) {

  $this->db->where("email", $em);
  $query = $this->db->get("members");
  
  $this->db->where("email", $em);
  $query2 = $this->db->get("verification");
  
  $this->db->where("email", $em);
  $query3 = $this->db->get("banned_email");
  
  $this->db->where("new_email", $em);
  $query4 = $this->db->get("verification_email");
  
  if (($query->num_rows()>0) or ($query2->num_rows()>0) or ($query3->num_rows()>0) or ($query4->num_rows()>0))
   {
   return true;
   }
   else
   {
   return false;
   }
  
}

Any ideas why the username remote query works but my email one doesn't? They essentially have the same functions so I am very confused...

Thanks
#2

[eluser]Dandy_andy[/eluser]
Have just done an error analysis and it appears the username is being posted but the email isn't. Any ideas why?
#3

[eluser]Aken[/eluser]
According to your jQuery, the selectors for your two fields have no distinguishing class or ID punctuation on them, just the names. How the username one matches, I don't know. But "em" is a valid HTML element, so it's looking for that rather than your field.

You need to have the ID hash in there.

Code:
data: $("#usernamenew").val()
#4

[eluser]Dandy_andy[/eluser]
Strangely, I tried the code with the hash and changed the 'em' field to 'mailadd' to avoid any HTML element issues and neither username or mailadd work. When I remove the hash from the username field (usernamenew), that one works fine but the email one still isn't working. I'm really baffled now...
#5

[eluser]Dandy_andy[/eluser]
Thank goodness for the Mozilla web developer error console. I've spotted whats wrong. It's actually an issue with the Model. One of the DB fields is incorrect and problem now all sorted.

Regarding the #hash before the fields, if I use a hash, the code doesn't work. But without a hash, it all works fine.
#6

[eluser]Aken[/eluser]
That's because it looks like you're setting the data parameter wrong. At least according to the documentation: http://docs.jquery.com/Plugins/Validatio...ods/remote




Theme © iAndrew 2016 - Forum software by © MyBB