TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-29-2009, 02:40 PM   #1 (permalink)
The Wanderer
 
adham is me's Avatar
 
Join Date: May 2009
Posts: 19
Thanks: 4
adham is me is on a distinguished road
Default Codeigniter & JQuery validation problem

hello everyone , i am trying to do some form validation using Condeiginter and Jquer's AJAX .

the view file
PHP Code:
</script>
<script type = "text/javascript">
    $(document).ready(function(){
        var dest = '<?php echo site_url("/users/fieldUpdate");?>';
     $('.item').editable(dest, {
         indicator : 'Saving...',
         tooltip   : 'Click to edit...',
         id   : 'fieldName',
         name : 'fieldValue',
         submit : 'Save',
         cancel : 'Undo',
         ajaxoptions: {
             dataType : 'json',
             success : function(data){
                 $("#response").html(data.msg).show(1000).slideDown();
             }
         }
        });
     });
</script>
<div id="response" class ="hidden">

</div>
    <label for ="username">Username : </label>
    <br>
    <h4 id="username" class ="item"><?php echo $userDetails['username'];?></h4>
    <br>
    <h4 id="password" class ="item"><?php echo $userDetails['password'];?></h4>
    <br>
    <h3 id="email"class ="item"><?php echo $userDetails['email'];?></h3>
    <h4 id="quote"class ="item"><?php echo $userDetails['quote'];?></h4>
    <br>
    <h4 id="avatarH4"><?php echo $userDetails['avatar'];?></h4>
the controller

PHP Code:

public function fieldUpdate(){

                if(isset(
$_POST['fieldName']) && isset($_POST['fieldValue'])){
                
                
// setting rules for CI validation class
            
$rules = array(
                               array(
                                     
'field'   => 'username',
                                     
'label'   => 'hblXhbl',
                                     
'rules'   => 'min_length[6]'
                                  
),
                                  array(
                                     
'field'   => 'password',
                                     
'label'   => 'Password',
                                     
'rules'   => 'required|min_length[5]|max_length[45]'
                                  
),
                                array(
                                     
'field'   => 'avtar',
                                     
'label'   => 'Avtar',
                                     
'rules'   => 'min_length[5]|max_length[105]|valid_url'
                                  
),
                                array(
                                     
'field'   => 'quote',
                                     
'label'   => 'Quote',
                                     
'rules'   => 'min_length[5]|max_length[100]'
                                  
)
                                  );
                
// getting proper rule
                
for($i=0;$i<5;$i++){
                if(
$_POST['fieldName'] == $rules[$i]['field']){
                    
// must be wrapped in an array to be in a valid format
                    
$fieldRules = array($rules[$i]);
                    break;
                    }
                }

                
$vArray $this->form_validation->metValidate($fieldRules);
                if(
$vArray[0]){
                    
// an array of DB field name and the new value of it
                    
$updateArray = array($_POST['fieldName']=>$_POST['fieldValue']);
                    
// no user can reach here untill he was signed in
                    
$userId $this->session->userdata("userid");
                    
// update userData in the DB
                    
$this->musers->updateUserdata($updateArray,$userId);
                    
// return a success msg
                    
$result = array("status"=>'success','msg'=>'userData updated !');
                }else{
                      
$result = array("status"=>'fail','msg'=>$vArray["validationErrors"]);
                }
                echo 
json_encode($result);
             }
    }

        
// a function used to update user data
        
public function personals(){
            
// check if the user is logged in
            
if(isLogged()){
                
$myArray whichStatus();
            
$data['status'] = $myArray[0];
                
$data['username'] = $myArray[1];
                
// the main view (a view contains user's personal information updatable using fieldUpdate() method)
                
$data['main']="Vpersonals";
                
$data['userDetails'] = $this->musers->getUserById(1,true);
                
$this->load->view("Vmain",$data);
              }else{
                    
// user is not logged in
                  
redirect("/","refresh");
              }
        } 
the validation method METvalidate()
PHP Code:
public $uniqueErrors = array();
    
    
    
// rules is an array of rules passed to the form validation class
    // uniques is an array of fields that must be unique
    
public function metValidate($rules,$uniques false){
        
$this->CI->form_validation->set_rules($rules);
            if(
$this->CI->form_validation->run()==false){
                            return array(
false,"validationErrors"=>validation_errors());
        }
        
        
// if no errors related with form validation then start checking uniques
        
if($uniques){
            
$this->CI->load->model("mglobal");
            
$tableName $uniques['tableName'];
            
            
// detecting non-unique fields 
            
foreach($uniques['uniques'] as $key=>$value){
                if(!
$this->CI->mglobal->isUnique($tableName,$key,$value)){
                    
$this->uniqueErrors[] = $value;
                }
            }
        if(
count($this->uniqueErrors)){
            return array(
false,"uniqueErrors"=>$this->uniqueErrors);
            }
        }
        
// if they are all fine just return true
        
return array(true);
    } 
the result that i get when executing this code is
1- when updating the username always i get userData updated as a 'msg' in the returned JSON object (even if the new username is lower the specified length or longer than it or even left blank !!).
2- any update in the password field results in the same message that says "The Password field is required".

so where is the problem !!
__________________
I love you NASSER
adham is me is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Basics or jQuery? devnull Javascript, AJAX, E4X 20 08-12-2009 03:12 PM
Huge Session Problem Killswitch General 1 11-17-2008 02:36 AM
jquery wont work for me :( sarmenhb Javascript, AJAX, E4X 2 09-27-2008 08:52 PM
Easy Peasy Variable Validation quantumkangaroo Absolute Beginners 3 03-20-2008 03:30 PM


All times are GMT. The time now is 10:11 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design