编辑表单使用codeigniter添加表单验证

时间:2014-05-13 13:20:21

标签: php forms codeigniter validation

您好我有这个编辑页面,当用户尝试编辑页面时,在编辑页面中我将其重定向到编辑页面本身,然后添加了一条说成功编辑的Flash消息。我的问题是我已经对它进行了验证,假设用户试图输入错误的电子邮件地址,它将验证无效的电子邮件。发生的事情是,它将验证无效的电子邮件地址,现在是电子邮件中的文本字段,数据仍然是一样的我怎么能正确设置到这个?有人能帮我吗?非常感谢。 这是我的控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Profile extends CI_Controller {
  public function __construct(){
    parent::__construct();   
    $this->load->library('session');
    $this->load->library('form_validation');
    $this->load->model('record_model', 'rm');
    if(!$this->session->userdata('loggedIn')){
      redirect('login');
    }
  }

  public function edit_successfully(){
    $this->form_validation->set_rules('firstname', 'First Name', 'required');
    $this->form_validation->set_rules('lastname', 'Last Name', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
    if($this->form_validation->run() == FALSE){
      $this->data['title'] = 'Edit Profile';
      $sessionData = $this->session->userdata('loggedIn');
      $this->data['id'] = $sessionData['id'];
      $this->data['query'] = $this->rm->getData($this->data['id']);
      $this->session->set_flashdata('error', 1);

      $this->load->view('templates/header', $this->data);
      $this->load->view('pages/edit');
      $this->load->view('templates/footer');
    }else{
      $sessionData = $this->session->userdata('loggedIn');
      $this->data['id'] = $sessionData['id'];
      $update = array(
                'first_name'=>$this->input->post('firstname'),
                'last_name'=>$this->input->post('lastname'),
                'email'=>$this->input->post('email')
                );
      $this->db->where('id', $this->input->post('editData'))->update('register', $update);
      $this->session->set_flashdata('success', 1);
      redirect(base_url().'profile/edit/id/'.$this->data['id']);
    }
  }

  public function edit($id){
    $this->data['title'] = 'Edit Profile';

    $sessionData = $this->session->userdata('loggedIn');
    $this->data['id'] = $sessionData['id'];
    $ID = $this->uri->segment(4);
    $this->data['query'] = $this->rm->getData($ID);

    $this->load->view('templates/header', $this->data);
    $this->load->view('pages/edit', $this->data);
    $this->load->view('templates/footer');

  }

  public function id($id){
    $this->data['title'] = 'View Profile';
    $this->data['viewProfile'] = 'view';

    $sessionData = $this->session->userdata('loggedIn');
    $this->data['id'] = $sessionData['id'];
    $this->data['query'] = $this->rm->getData($id);

    $this->load->view('templates/header',$this->data);
    $this->load->view('pages/profile', $this->data);
    $this->load->view('templates/footer');

  }

    public function index(){
    $this->data['title'] = 'Profile';

    $this->load->view('templates/header', $this->data);
    $this->load->view('pages/profile', $this->data);
    $this->load->view('templates/footer');   
  }

}

这是我的观点

<div class="container">
  <div class="welcome_content"> 
    <?php echo $this->load->view('menu/menu'); ?>
    <div class="clearfix"></div>
    <?php if($this->session->flashdata('success')): ?>
      <div class="alert alert-success">
        <a href="#" class="close" data-dismiss="alert">&times;</a>
        <strong>Success!</strong> Successfully Edited.
      </div>
    <?php endif; ?>
    <div class="profile_content">
      <?php if($this->session->flashdata('error')): ?>
        <form action="<?php echo base_url().'profile/edit_successfully'?>" method="post">
          <div class="error">
            <?php echo validation_errors('<div class="error">','</div>');?>
          </div>
          FirstName:<input type="text" name="firstname" value="<?php echo $query->first_name;?>" /><br /><br />
          LastName:<input type="text" name="lastname" value="<?php echo $query->last_name; ?>" />
          <br />
          <br />
          Email:<input type="text" name="email" value="<?php echo $query->email; ?>" />
          <br />
          <br />
          <input type="hidden" name="editData" value="<?php echo $query->id; ?>" />
          <input type="submit" value="Edit" class="btn btn-primary"/>
        </form>
      <?php else: ?>
        <form action="<?php echo base_url().'profile/edit_successfully'?>" method="post">
          <div class="error">
            <?php echo validation_errors('<div class="error">','</div>');?>
          </div>
          FirstName:<input type="text" name="firstname" value="<?php echo $query->first_name;?>" /><br /><br />
          LastName:<input type="text" name="lastname" value="<?php echo $query->last_name; ?>" />
          <br />
          <br />
          Email:<input type="text" name="email" value="<?php echo $query->email; ?>" />
          <br />
          <br />
          <input type="hidden" name="editData" value="<?php echo $query->id; ?>" />
          <input type="submit" value="Edit" class="btn btn-primary"/>
        </form>
      <?php endif; ?>
      <a href="<?php echo base_url().'homepage'?>" title="Back">Back</a>
    </div>
  </div>
</div>

现在我在表格中的视图中添加了flashdata错误。当用户尝试更改电子邮件地址或名字或姓氏时执行。 有人可以帮我解决这个问题吗?

0 个答案:

没有答案