使用Codeigniter进行表单验证时出错

时间:2018-09-11 10:36:27

标签: php codeigniter

我在使用codeigniter验证表单时遇到问题。下面是我在项目中使用的代码。

我遵循了cdoeigniter用户指南中已阅读的所有规则,但是不知道是什么问题,验证没有发生。

控制器(page.php)             

        class Page extends Front_Controller
        {

            public function __construct()
            {
                parent::__construct();

                $this->load->model('Page_model');
                $this->load->helper(array('form', 'url'));
            }

            public function index()
            {
                $data['page_title'] = 'Doctors Feedback';
                $data['base_url']= $this->uri->segment_array();

                $this->load->library('form_validation');
                $this->form_validation->set_rules('name', 'Name', 'required');
                $this->form_validation->set_rules('qualification', 'Qualification', 'required');
                $this->form_validation->set_rules('age', 'Age', 'required');
                $this->form_validation->set_rules('date', 'Date', 'required');
                $this->form_validation->set_rules('residential', 'Residential Address', 'required');
                $this->form_validation->set_rules('clinic', 'Clinic Address', 'required');
                $this->form_validation->set_rules('email', 'E-mail', 'required');
                $this->form_validation->set_rules('mobile', 'Mobile Number', 'required');
                $this->form_validation->set_rules('phone', 'Phone Number', 'required');
                $this->form_validation->set_rules('comment', 'Comment', 'required');
                if ($this->form_validation->run() == FALSE)
                {
                    $this->view('doctors_form', $data);
                }
            }
            }
            ?>

view(doctors_form.php)

    <form>
        <div class="col-md-12 form-group">
                        <label>Name</label>
                        <input type="text" class="form-control" id="name" name="name" value="<?php echo set_value('name')?>" placeholder="Name">
                        <span class="text-danger"><?php echo form_error("name");?></span>
        </div>
        <div class="col-md-6 form-group">
                        <label>Select Gender</label>
                        <select class="form-control jcf-hidden" data-jcf="{&quot;wrapNative&quot;: false, &quot;wrapNativeOnMobile&quot;: false}">
                            <option value="male">Male</option>
                            <option value="female">Female</option>
                        </select>
        </div>
        <div class="col-md-6 form-group">
                        <label>Qualification</label>
                        <input type="text" class="form-control" id="qualification" name="qualification" value="<?php echo set_value('qualification')?>" placeholder="Qualification">
                        <span class="text-danger"><?php echo form_error("qualification");?></span>
        </div>
        <div class="col-md-6 form-group">
                        <label>Age</label>
                        <input type="text" class="form-control" id="age" name="age" value="<?php echo set_value('age')?>" placeholder="Age">
                        <span class="text-danger"><?php echo form_error("age");?></span>
                    </div>
        <div class="col-md-6 form-group">
                        <label>Date</label>
                        <input type="text" class="form-control" id="date" name="date" value="<?php echo set_value('date')?>" placeholder="Date">
                        <span class="text-danger"><?php echo form_error("date");?></span>
        </div>
        <div class="col-md-12 form-group">
                        <label>Residential Address</label>
                        <textarea rowa="5" class="form-control" id="address" name="residential" placeholder="Residential Address"></textarea>
                        <span class="text-danger"><?php echo form_error("residential");?></span>
                    </div>
        <div class="col-md-12 form-group">
                        <label>Clinic Address</label>
                        <textarea rowa="5" class="form-control" id="address" name="clinic" placeholder="Clinic Address"></textarea>
                        <span class="text-danger"><?php echo form_error("clinic");?></span>
                    </div>
        <div class="col-md-12 form-group">
                        <label>Email</label>
                        <input type="text" class="form-control" id="email" name="email" value="<?php echo set_value('email')?>" placeholder="Email">
                        <span class="text-danger"><?php echo form_error("email");?></span>
                    </div>
        <div class="col-md-6 form-group">
                        <label>Mobile</label>
                        <input type="text" class="form-control" id="mobile" name="mobile" value="<?php echo set_value('mobile')?>" placeholder="Mobile">
                        <span class="text-danger"><?php echo form_error("mobile");?></span>
                    </div>
        <div class="col-md-6 form-group">
                        <label>Phone</label>
                        <input type="text" class="form-control" id="phone" name="phone" value="<?php echo set_value('phone')?>" placeholder="Phone">
                        <span class="text-danger"><?php echo form_error("phone");?></span>
                    </div>
        <div class="col-md-12 form-group">
                        <label>Comment</label>
                        <textarea rowa="5" class="form-control" id="comment" name="comment" placeholder="Comment"></textarea>
                        <span class="text-danger"><?php echo form_error("comment");?></span>
                    </div>
        <div class="col-md-12 text-right">
                        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
        </div>
    </form>

1 个答案:

答案 0 :(得分:0)

首先,您应该将控制器名称重命名为Page.php(首字母大写)。您的控制器代码应如下所示:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Page extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();

        $this->load->model('Page_model');
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
    }

    public function index()
    {
        if ($_POST) 
        {
            $this->form_validation->set_rules('name', 'Name', 'required');
            $this->form_validation->set_rules('qualification', 'Qualification', 'required');
            $this->form_validation->set_rules('age', 'Age', 'required');
            $this->form_validation->set_rules('date', 'Date', 'required');
            $this->form_validation->set_rules('residential', 'Residential Address', 'required');
            $this->form_validation->set_rules('clinic', 'Clinic Address', 'required');
            $this->form_validation->set_rules('email', 'E-mail', 'required');
            $this->form_validation->set_rules('mobile', 'Mobile Number', 'required');
            $this->form_validation->set_rules('phone', 'Phone Number', 'required');
            $this->form_validation->set_rules('comment', 'Comment', 'required');

            if ($this->form_validation->run() == FALSE)
            {
                $data['page_title']         = 'Doctors Feedback';
                $data['base_url']           = $this->uri->segment_array();
                $this->view('doctors_form', $data);
            }
            else
            {
                $name           = $this->input->post('name',TRUE);
                $qualification  = $this->input->post('qualification',TRUE);
                $age            = $this->input->post('age',TRUE);
                $date           = $this->input->post('date',TRUE);
                $residential    = $this->input->post('residential',TRUE);
                $clinic         = $this->input->post('clinic',TRUE);
                $email          = $this->input->post('email',TRUE);
                $mobile         = $this->input->post('mobile',TRUE);
                $phone          = $this->input->post('phone',TRUE);
                $comment        = $this->input->post('comment',TRUE);

                $data = array(
                    'name'          => $name , 
                    'qualification' => $qualification , 
                    'age'           => $age , 
                    'date'          => $date , 
                    'residential'   => $residential , 
                    'clinic'        => $clinic , 
                    'email'         => $email , 
                    'mobile'        => $mobile , 
                    'phone'         => $phone , 
                    'comment'       => $comment , 
                );

                // Further deal with model using this array
            }
        }
    }


}

您认为,最好使用codeigniter形式的open和close标签代替HTML。

<?php echo form_open('Controller_name/function_name'); ?>  // equivalent to <form action='url'>
<?php echo form_close(); ?>  // equivalent to </form>

就您而言,您不必在form_open标记中编写函数名称。只需写控制器名称,因为默认情况下会加载索引函数。