CodeIgniter Forums

Full Version: Problem with Form Validation when update field
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]PEN Vannak[/eluser]
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.

El Forum

[eluser]Thorpe Obazee[/eluser]
Really like to help but I couldn't follow the explanation.

El Forum

[eluser]PEN Vannak[/eluser]
Hmmm.........
I would like to show you about my code here!
+Runction in Controller page called "member_management"
<controller>
Code:
&lt;?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);    
        }
    }
?&gt;
</controller>
********************************************************************************************************

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

El Forum

[eluser]Thorpe Obazee[/eluser]
Please use the [ code] tags

El Forum

[eluser]PEN Vannak[/eluser]
+Code in Viewers
<viewers>
Code:
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]
&lt;?php
    //label link Go Back
    echo anchor('member_management/index','Back');
?&gt;
&lt;?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).' <img id="img1" alt="" src="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).' <img id="img2" alt="" src="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).' <img id="img3" alt="" src="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).' <img id="img4" alt="" src="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'.' <img id="img5" alt="" src="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).' <img id="img6" alt="" src="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'.' <img id="img7" alt="" src="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'.' <img id="img8" alt="" src="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>&nbsp;</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','&lt;input type="button" value="Cancel"');
                echo '&lt;/td>';
            echo '</tr>';
        echo '</table>';    
    echo form_close();
}else{
       echo 'Now date to view.';
}
    //Close form
?&gt;
</viewers>

El Forum

[eluser]PEN Vannak[/eluser]
+Code in Models
<models>
Code:
&lt;?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);
    }
?&gt;
</models>

El Forum

[eluser]Thorpe Obazee[/eluser]
PEN Vannak, please see post #3.