javascript - jQuery Validation plugin remote rule changes php function value -
i have "edit user form" in order change user data when needed. 1 of fields id number. use jquery validation plugin besides codeigniter's form validation library.
i have php function in admin check if id entered user exists on database. if id matches 1 user has stored on database it's fine. when trying put user's id, error shows up. if trying enter id it's not stored on database should ok , no error should displayed.
i build php function working fine. problem when doing jquery function call php function.
here's view:
<div class="span6">   <div class="control-group">     <label class="control-label">número<span class="required">*</span></label>       <div class="controls">         <input type="text" name="documentn" id="documentn" class="m-wrap span8" value="{$frontuser->document}" required/>             <span class="help-block"></span>       </div>   </div> </div>   here's jquery function:
documentn: {   required: true,   minlength: 7,   maxlength: 20,   remote: {   url: '/admin/checkdocanduser',   type: 'post',    },    },   here's php function:
public function checkdocanduser(){     if ((isset($_post['documentn'])) && (isset($_post['id']))) {         $n = $_post['documentn'];         $id = $_post['id'];         $dn = usermanager::getinstance()->checkuserdocument($n,$id);         if ($dn) {             //id belongs user             echo "true";         }else{             //does id entered belongs user?             $exists = usermanager::getinstance()->getbydocument($_post['documentn']);             if (!$exists) {                 // number entered belongs user                 echo "false";             }         }     } }   so given php function works when don't use js validation file...why jquery function not working?
i have tried changing remote's rule url to: admin/checkdocanduser, /checkdocanduser, checkdocanduser doesn't work.
edit , working
considered @sparky said, , i'm sending user id
documentn: {   required: true,   minlength: 7,   maxlength: 20,   remote: {     url: '/admin/checkdocanduser',     type: 'post',     data: {      id: function(){      var dn = $('#id').val();      return dn;     }                                    }    } },  public function checkdocanduser(){     if ((isset($_post['documentn'])) && (isset($_post['id']))) {         $dn = usermanager::getinstance()->checkuserdocument($_post['documentn'],$_post['id']);         if ($dn) {             //id belongs user             echo "true";         }else{             //does id entered belong user?             $exists = usermanager::getinstance()->getbydocument($_post['documentn']);             if (!$exists) {                 // number entered belongs user                 echo "true";             }else{                 echo "false";             }         }     } }      
1) verify you're including jquery validation plugin on page. needs included after jquery library.
2)  in php function, seem looking two post inputs: if ((isset($_post['documentn'])) && (isset($_post['id']))).  however, default remote method sends data field you're validating, documentn.  if need value field sent along, need configure using data option in remote...
documentn: {     required: true,     // minlength: 7,     // <- can replaced rangelength rule     // maxlength: 20,    // <- can replaced rangelength rule     rangelength: [7,20], // <- can used in place of 2 rules     remote: {            // <- default, posts data 'documentn' field                url: '/admin/checkdocanduser',         type: 'post',         data: {          // <- post data field(s)             anotherfield: function() {  // <- post name, $_post['anotherfield']                 return $( "#another_field" ).val();             }         }     }   },   3) i'm not sure logic in checkdocanduser function.  happens when not belong other user , exists?  you're not echoing in case.  remote method needs true when validation should pass , false, undefined, or null when should fail.  if echoes json string, fails , string becomes error message.  verify logic doing intended as per docs.
Comments
Post a Comment