Welcome Guest, Not a member yet? Register   Sign In
Help with Validation and Form Appearing Twice
#1

[eluser]TerryT[/eluser]
I am trying to allow an admin to edit a user's data. After a couple of hours, I don't think I can screw this up any worse. The code below prints the forms out twice. Can someone tell me why? It appears the validation is returning FALSE prior to me pressing the submit button on the form. Using Ion Auth by the way.

Controller:
Code:
function edit_user($id)
        {
        //Get the specific user data:
        $data['user'] = $this->ion_auth->get_user_array($id);

        //Break the phone number into three parts:
        $phone = explode("-", $data['user']['phone']);
        $data['user']['phone1'] = $phone[0];
        $data['user']['phone2'] = $phone[1];
        $data['user']['phone3'] = $phone[2];
        
        $this->load->view('auth/edit_user', $data);
    
        // validation rules
    $this->form_validation->set_rules('first_name', 'First Name', 'trim|required|min_length[2]|max_length[50]|xss_clean');
    $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|min_length[2]|max_length[50]|xss_clean');
        $this->form_validation->set_rules('company', 'Company', 'trim|required|min_length[2]|max_length[100]|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|min_length[6]|max_length[100]|xss_clean');
        $this->form_validation->set_rules('phone1', 'Phone1', 'trim|required|min_length[1]|max_length[6]|xss_clean');
        $this->form_validation->set_rules('phone2', 'Phone2', 'trim|required|min_length[1]|max_length[6]|xss_clean');
        $this->form_validation->set_rules('phone3', 'Phone3', 'trim|required|min_length[1]|max_length[6]|xss_clean');
        
        if ($this->form_validation->run() == FALSE)  //If it doesn't validate, reload the edit_user_view.
        {

        $this->load->view('auth/edit_user');
        }     else {
            //Put the phone number back together:
            $phonenum = $this->input->post('phone1') . '-' . $this->input->post('phone2') . '-' . $this->input->post('phone3');
            
            $data1 = array(
                'first_name'=>$this->input->post('first_name'),  //Using Active Record, the values are automatically escaped. twt 14Oct10.
                'last_name'=>$this->input->post('last_name'),
                'company'=>$this->input->post('company'),
                'email'=>$this->input->post('email'),
                'phone'=>$phonenum
        );
            
            $this->ion_auth->update_user($id, $data1);
                
            }
            redirect('Users/');  //Redirects back to the users_view page.
        }

View:
Code:
<div class='mainInfo'>

    <h1>Edit User</h1>
    <p>Please edit any of the users information below. All fields are required.</p>
    &lt;?php echo validation_errors(); ?&gt;
    
        &lt;form method="post" action="&lt;?php echo site_url('Users/edit_user/' . $this-&gt;uri->segment(3))?&gt;" >
  
        
      <p>First Name:<br />
            &lt;input type="text" name="firstname" id="firstname" value= "&lt;?php echo $user['first_name']; ?&gt;" maxlength="50" size="20"  /&gt;
      </p>
      
      <p>Last Name:<br />
      &lt;input type="text" name="lastname" id="lastname" value= "&lt;?php echo $user['last_name']; ?&gt;" maxlength="50" size="20"  /&gt;
      </p>
      
      <p>Company Name:<br />
      &lt;input type="text" name="company" id="company" value= "&lt;?php echo $user['company']; ?&gt;" maxlength="100" size="20"  /&gt;
      </p>
      
      <p>Email:<br />
      &lt;input type="text" name="email" id="email" value= "&lt;?php echo $user['email']; ?&gt;" maxlength="100" size="20"  /&gt;
      </p>
      
      <p>Phone:<br />
      &lt;input type="text" name="phone1" id="phone1" value= "&lt;?php echo $user['phone1']; ?&gt;" maxlength="6" size="6"  /&gt;-
            &lt;input type="text" name="phone2" id="phone2" value= "&lt;?php echo $user['phone2']; ?&gt;" maxlength="6" size="6"  /&gt;-
            &lt;input type="text" name="phone3" id="phone3" value= "&lt;?php echo $user['phone3']; ?&gt;" maxlength="6" size="6"  /&gt;
      </p>
      
            <p>&lt;input type="submit" value="Edit User"/&gt;&lt;/p>
      
            
            <p> </p>
            <p><a href="&lt;?php echo base_url(); ?&gt;auth/">Return to Main Page</a></p>
            <p> </p>
            <p><a href="&lt;?php echo base_url(); ?&gt;auth/logout">Log Out</a></p>

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


Messages In This Thread
Help with Validation and Form Appearing Twice - by El Forum - 10-29-2010, 07:14 AM
Help with Validation and Form Appearing Twice - by El Forum - 10-29-2010, 08:51 AM
Help with Validation and Form Appearing Twice - by El Forum - 10-29-2010, 09:19 AM



Theme © iAndrew 2016 - Forum software by © MyBB