简体   繁体   English

使用CodeIgniter的PHP表单验证

[英]PHP form validation using CodeIgniter

I have a simple form and applied validation to using CodeIgniter's form_validation->set_rules . 我有一个简单的表单,并将验证应用于使用CodeIgniter的form_validation->set_rules

It works fine but the only issue is when I submit the form with one field blank, it shows a message to fill in that field, but the others fields also become blank. 它工作正常,但是唯一的问题是当我提交一个字段为空白的表单时,它显示一条消息以填充该字段,而其他字段也变为空白。

How can I solve this so that when I submit the form with one field blank, it should show a message for that field, and other fields should hold the data entered in it. 我该如何解决这个问题,以便当我提交一个字段为空的表单时,它应该显示该字段的消息,而其他字段应该包含在其中输入的数据。

Below is the code: 下面是代码:

View page: 查看页面:

<form name='frm1' action="<?php echo base_url(); ?>index.php/Registrationc/add_user"  id="form1" method="post">
   <input type="text" class="form-control" style="text-transform: capitalize;" autocomplete="off" id="fname" name="fname">
   <h6 style="color:red;"><?php echo form_error('fname');?></h6>

   <input type="text" class="form-control" style="text-transform: capitalize;" autocomplete="off" id="mname" name="mname">
   <h6 style="color:red;"> <?php echo form_error('mname');?></h6>

   <input type="text" class="form-control" style="text-transform: capitalize;" autocomplete="off" id="lname" name="lname">
   <h6 style="color:red;"><?php echo form_error('lname');?></h6>

   <center>
      <input type="submit" value="Submit" class="btn btn-bricky" id="subbtn" name="submit">
   </center>
</form> 

Controller: 控制器:

public function add_patient() {

    $this->form_validation->set_rules('fname', 'Firstname', 'trim|required|alpha');
    $this->form_validation->set_rules('mname', 'Middlename', 'trim|required|alpha');
    $this->form_validation->set_rules('lname', 'Lastname', 'trim|required|alpha');

    if ($this->form_validation->run() == FALSE) {
        $this->extra3();
    } else {
        $this->load->model('addpatientM');
        $fname1 = $this->input->post('fname');
        $lname1 = $this->input->post('lname');
        $mname1 = $this->input->post('mname');

        $submit = $this->addpatientM->insert_patient($fname1, $lname1, $mname1);
        if ($submit === true) {
            $this->load->library('session');
            $this->session->set_flashdata('success', 'successfully added');
        } else {
            $this->session->set_flashdata('fail', 'Opps there was some error while inserting');
        }
        redirect('Registrationc/extra3');
    }
}

You need to echo set_value() into the value-attribute. 您需要将set_value()到value属性中。

First you need to load form - helper: 首先,您需要加载表格-帮助程序:

 $this->load->helper('form'); 

Update: Actually if you're using the form_validation library and using validation rules for the field in question, then there's no need to load the form helper separately for set_value . 更新:实际上,如果您使用的是form_validation库,并且对相关字段使用了验证规则,则无需为set_value单独加载form助手。

Then use set_value in your view to set the value: 然后在您的视图中使用set_value设置值:

<input type="text" class="form-control" style="text-transform: capitalize;" 
autocomplete="off" id="lname" name="lname" value=<?= set_value("lname"); ?>>

Form Helper: Codeigniter documentation 表单助手:Codeigniter文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM