ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Problem with Form Validation when update field

June 17, 2009 3:07am

Subscribe [1]
  • #1 / Jun 17, 2009 3:07am

    PEN Vannak

    14 posts

    Dear CodeIgniter,

    My name is PEN Vannak. I am a beginning developer and start my project with the CodeIgniter at the first time.

    I have the problem with the form validation when I update the field.

    Lets me describe about my problems:
      -I have the page Member Management.
      -I want to update my field in this page. But when I edit, first I have to check the empty field and check valid email. If I edited in the correct format(fill all required field and right email format), it is successful.
      -But if I edited in the wrong format(didn’t fill all required field, or wrong email format), I want it to show the error message, and show the field that I should edit.

    I have found that in the page viewers, I have input hidden field call(mem_id) to call the field for edit.

    In the controllers, I have the function called “edit_member” that have the one parameter is “mem_id”.

    When I edit first, it call the field into function with the “mem_id” according to the field I have selected. But when it I input the incorrect form format, this function will be called again and include eith the “mem_id”. In this case, the “mem_id” is equal to 0. So it could not select the field for edit again.

    If any one understand this problem, or have experience with this. Please explain me!

    I’m waiting to get all advice from you all.

    Best regard,
    PEN Vannak.

  • #2 / Jun 17, 2009 4:11am

    Thorpe Obazee

    1138 posts

    Really like to help but I couldn’t follow the explanation.

  • #3 / Jun 17, 2009 4:37am

    PEN Vannak

    14 posts

    Hmmm…......
    I would like to show you about my code here!
    +Runction in Controller page called “member_management”
    <controller>

    <?php
           function edit_member($mem_id=0){
            $this->load->helper(array('form', 'url'));
            $this->load->helper('email');
            if(($this->input->post('mem_name')) || ($this->input->post('ide_number')) || ($this->input->post('mem_phone')) || ($this->input->post('mem_email'))){
                if(valid_email($this->input->post('mem_email'))){
                    $this->member->edit_member();
                    redirect('member_management','refresh');
                }else{
                    $data['invalid_email']='<font color="#ccccff>"*Invalid email.</font>';
                    $data['mem_name']   = array('id' => 'mem_name', 'name' => 'mem_name' ,'onblur'=>'[removed]check_empty(this,1,id);','style'=>'width: 140px','value'=>'mem_name');
                    $data['ide_number']   = array('id' => 'ide_number', 'name' => 'ide_number' ,'onblur'=>'[removed]check_empty(this,2,0);','style'=>'width: 140px');
                    $data['mem_phone']   = array('id' => 'mem_phone', 'name' => 'mem_phone' ,'onblur'=>'[removed]check_empty(this,3,id);','style'=>'width: 140px');
                    $data['mem_email']   = array('id' => 'mem_email', 'name' => 'mem_email','onblur'=>'[removed]check_empty(this,4,id);','style'=>'width: 140px');
                    $data['mem_date_of_birth']   = array('id' => 'mem_date_of_birth', 'name' => 'mem_date_of_birth' ,'onblur'=>'[removed]check_empty(this,5,id);','style'=>'width: 140px');
                    $data['mem_place_of_birth']   = array('id' => 'mem_place_of_birth', 'name' => 'mem_place_of_birth' ,'onblur'=>'[removed]check_empty(this,6,id);','style'=>'width: 140px');
                    $data['mem_start_date_member']   = array('id' => 'mem_start_date_member', 'name' => 'mem_start_date_member' ,'onblur'=>'[removed]check_empty(this,7,id);','style'=>'width: 140px');
                    $data['mem_expire_date_member']   = array('id' => 'mem_expire_date_member', 'name' => 'mem_expire_date_member' ,'onblur'=>'[removed]check_empty(this,8,id);','style'=>'width: 140px');
                    $data['ide_name']=$this->sel_identity();
                    $data['base'] = $this->config->item('base_url');
                    $data['css'] = $this->config->item('css');
                    $data['name']=$_SESSION['use_name'];
                    $data['main']='member/member_edit';
                    $data['title']='Edit Member';
                    $data['calender_helper']='content/js/help_script_calendar.js';
                    $data['member_detail']=$this->member->detail_member($mem_id);
                    if($_SESSION['user_level']==1){
                        $data['sidebar']='sidebar';
                    }else{
                        $data['sidebar']='sidebar_manager';
                    }
                    $this->load->vars($data);
                    $this->load->view('template',$data);    
                }
            }else{
                $data['invalid_email']='<font color="#ccccff>"*Invalid email.</font>';
                $data['mem_name']   = array('id' => 'mem_name', 'name' => 'mem_name' ,'onblur'=>'[removed]check_empty(this,1,id);','style'=>'width: 140px');
                $data['ide_number']   = array('id' => 'ide_number', 'name' => 'ide_number' ,'onblur'=>'[removed]check_empty(this,2,0);','style'=>'width: 140px');
                $data['mem_phone']   = array('id' => 'mem_phone', 'name' => 'mem_phone' ,'onblur'=>'[removed]check_empty(this,3,id);','style'=>'width: 140px');
                $data['mem_email']   = array('id' => 'mem_email', 'name' => 'mem_email','onblur'=>'[removed]check_empty(this,4,id);','style'=>'width: 140px');
                $data['mem_date_of_birth']   = array('id' => 'mem_date_of_birth', 'name' => 'mem_date_of_birth' ,'onblur'=>'[removed]check_empty(this,5,id);','style'=>'width: 140px');
                $data['mem_place_of_birth']   = array('id' => 'mem_place_of_birth', 'name' => 'mem_place_of_birth' ,'onblur'=>'[removed]check_empty(this,6,id);','style'=>'width: 140px');
                $data['mem_start_date_member']   = array('id' => 'mem_start_date_member', 'name' => 'mem_start_date_member' ,'onblur'=>'[removed]check_empty(this,7,id);','style'=>'width: 140px');
                $data['mem_expire_date_member']   = array('id' => 'mem_expire_date_member', 'name' => 'mem_expire_date_member' ,'onblur'=>'[removed]check_empty(this,8,id);','style'=>'width: 140px');
                $data['ide_name']=$this->sel_identity();
                $data['base'] = $this->config->item('base_url');
                $data['css'] = $this->config->item('css');
                $data['name']=$_SESSION['use_name'];
                $data['main']='member/member_edit';
                $data['title']='Edit Member';
                $data['calender_helper']='content/js/help_script_calendar.js';
                $data['member_detail']=$this->member->detail_member($mem_id);
                if($_SESSION['user_level']==1){
                    $data['sidebar']='sidebar';
                }else{
                    $data['sidebar']='sidebar_manager';
                }
                $this->load->vars($data);
                $this->load->view('template',$data);    
            }
        }
    ?>

    </controller>
    ********************************************************************************************************

    Please wait to see the page viewers in the next post.

  • #4 / Jun 17, 2009 4:40am

    Thorpe Obazee

    1138 posts

    Please use the [ code] tags

  • #5 / Jun 17, 2009 4:47am

    PEN Vannak

    14 posts

    +Code in Viewers
    <viewers>

    function check_empty(obj,i){
            frm=document.forms[0];
            if(obj.value==""){
                document.getElementById('img'+i).src="images/icon_cross.gif";
            }else{
                document.getElementById('img'+i).src="images/icon_check.gif";
            }
        }
    [removed]
    <?php
        //label link Go Back
        echo anchor('member_management/index','Back');
    ?>
    <?php
    echo '<font color="#cc0000">'.validation_errors().'</font>';
    if(count($member_detail)){
        $attribute=array('name'=>'form_edit','id'=>'form_edit');
        echo form_open('member_management/edit_member',$attribute);
            echo '<table border="0" cellpadding="0" cellspacing="0">';
                echo '<tr>';
                    echo '<td>Member Name:<font color="#cc0000">*</font></td>';
                    echo '<td>';
                        $mem_name['value']=$member_detail['mem_name'];
                        echo form_input($mem_name).' images/blank.gif';
                    echo '</td>';
                echo '</tr>';
                echo '<tr>';
                    echo '<td>Member Sex:</td>';
                    echo '<td>';
                        $options = array('male'=>'Male','female'=>'Female');
                echo form_dropdown('mem_sex',$options);
                    echo '</td>';
                echo '</tr>';
                echo '<tr>';
                    echo '<td>Identity Name:</td>';
                    echo '<td>';
                        echo form_dropdown('ide_id',$ide_name);
                    echo '</td>';
                echo '</tr>';
                echo '<tr>';
                    echo '<td>Identity Number:<font color="#cc0000">*</font></td>';
                    echo '<td>';
                        $idi_number['value']=$member_detail['ide_number'];
                        echo form_input($ide_number).' images/blank.gif';
                    echo '</td>';
                echo '</tr>';
                echo '<tr>';
                    echo '<td>Phone Number:<font color="#cc0000">*</font></td>';
                    echo '<td>';
                        $mem_phone['value']=$member_detail['mem_phone'];
                        echo form_input($mem_phone).' images/blank.gif';
                    echo '</td>';
                echo '</tr>';
                echo '<tr>';
                    echo '<td>Email:</td>';
                    echo '<td>';
                        $mem_email['value']=$member_detail['mem_email'];
                        echo form_input($mem_email).' images/blank.gif';
                    echo '</td>';
                echo '</tr>';
                echo '<tr>';
                    echo '<td>Date of Birth:<font color="#cc0000">*</font></td>';
                    echo '<td>';
                        $mem_date_of_birth['value']=$member_detail['mem_date_of_birth'];
                        echo form_input($mem_date_of_birth);
                        echo ' Format:yy/mm/dd'.' images/blank.gif';
                    echo '</td>';
                echo '</tr>';
                echo '<tr>';
                    echo '<td>Place of Bith:<font color="#cc0000">*</font></td>';
                    echo '<td>';
                        $mem_place_of_birth['value']=$member_detail['mem_place_of_birth'];
                        echo form_input($mem_place_of_birth).' images/blank.gif';
                    echo '</td>';
                echo '</tr>';
                echo '<tr>';
                    echo '<td>Start Member Date:<font color="#cc0000">*</font></td>';
                    echo '<td>';
                        $mem_start_date_member['value']=$member_detail['mem_start_date_member'];
                        echo form_input($mem_start_date_member);
                        echo ' Format:yy/mm/dd'.' images/blank.gif';
                    echo '</td>';
                echo '</tr>';
                echo '<tr>';
                    echo '<td>Expire Date Member:<font color="#cc0000">*</font></td>';
                    echo '<td>';
                        $mem_expire_date_member['value']=$member_detail['mem_expire_date_member'];
                        echo form_input($mem_expire_date_member);
                        echo ' Format:yy/mm/dd'.' images/blank.gif';
                    echo '</td>';
                echo '</tr>';
                echo '<tr>';
                    echo '<td>Status:</td>';
                    echo '<td>';
                        $options = array('1' => 'Published', '0' => 'Unpublished');
                        echo form_dropdown('status',$options);
                    echo '</td>';
                echo '</tr>';
                echo '<tr><td> </td></tr>';
                echo '<tr>';
                    echo '<td align="center" colspan="2">';
                        echo form_hidden('mem_id',$member_detail['mem_id']);
                        echo form_submit('submit','Update');
                        echo anchor('member_management/index','<input type="button" value="Cancel"');
                    echo '</td>';
                echo '</tr>';
            echo '</table>';    
        echo form_close();
    }else{
           echo 'Now date to view.';
    }
        //Close form
    ?>

    </viewers>

  • #6 / Jun 17, 2009 4:48am

    PEN Vannak

    14 posts

    +Code in Models
    <models>

    <?php
         function detail_member($mem_id){
            $data=array();
            $this->db->select('mem_id,
                                ide_number,
                                mem_name, 
                                mem_sex, 
                                mem_phone, 
                                mem_email,
                                mem_date_of_birth,
                                mem_place_of_birth, 
                                mem_start_date_member, 
                                mem_expire_date_member,
                                mem_date_created,
                                mem_date_modified,
                                lib_identities.status,
                                lib_identities.ide_name AS identity');
            $this->db->join('lib_identities','lib_members.ide_id=lib_identities.ide_id');
            $this->db->where('mem_id',$mem_id);
            $this->db->from('lib_members');
            $this->db->limit(1);
            $query=$this->db->get();
            if($query->num_rows()>0){
                $data=$query->row_array();
            }
            $query->free_result();
            return $data;
        }
    
            function edit_member(){
            $now = date("Y-m-d H:i:s");
            $data = array(
            'ide_id' => $this->input->post('ide_id'),
            'ide_number' => $this->input->post('ide_number'),
            'mem_name' => $this->input->post('mem_name'),
            'mem_sex' => $this->input->post('mem_sex'),
            'mem_phone'=> $this->input->post('mem_phone'),
            'mem_email'=> $this->input->post('mem_email'),
            'mem_date_of_birth'=> $this->input->post('mem_date_of_birth'),
            'mem_place_of_birth'=> $this->input->post('mem_place_of_birth'),
            'mem_start_date_member'=> $this->input->post('mem_start_date_member'),
            'mem_expire_date_member'=> $this->input->post('mem_expire_date_member'),
            'mem_date_modified'=> $now,
            'status'=> $this->input->post('status')
            );
            $this->db->where('mem_id',$_POST['mem_id']);
            $this->db->update('lib_members', $data);
        }
    ?>

    </models>

  • #7 / Jun 17, 2009 4:49am

    Thorpe Obazee

    1138 posts

    PEN Vannak, please see post #3.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases