【问题标题】:CodeIgniter form validation whitout reload page无需重新加载页面的 CodeIgniter 表单验证
【发布时间】:2015-06-22 15:34:24
【问题描述】:

我在 CodeIgniter 中有一个完美运行的表单,我只是有一个小问题,如果不重新加载我的页面就无法显示验证,有没有简单的方法来解决这个问题?

控制器:

用户.php

<?php
/* 
 * File Name: user.php
 */


if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->database();
        $this->load->library('form_validation');
        //load the user model
        $this->load->model('User_model');
    }

    //index function
    function index()
    {

        //fetch data from country table
      $data['country'] = $this->User_model->get_country();


        //set validation rules
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('name', 'Name','trim|required|min_length[5]|max_length[20]');

        $this->form_validation->set_rules('country', 'Country', 'callback_combo_check');
        $this->form_validation->set_rules  ('passconf', 'Password Confirmation', 'trim|required');




        if ($this->form_validation->run() == FALSE)
        {
            //fail validation
            $this->load->view('user_view2', $data);
        }
        else
        {    
            //pass validation
            $data = array(
                'user_email' => $this->input->post('email'),
                'user_name' => $this->input->post('name'),
                'countryCode' => $this->input->post('country'), //inserting id
                'Password' => $this->input->post('passconf'),
                //encrypting pass on database.
                'Password' => password_hash('passconf', PASSWORD_BCRYPT),

            );


            $this->db->insert('ci', $data);

            //display success message
            $this->session->set_flashdata('msg', '<div class="alert alert-success text-center">You Are Now Registered</div>');
            redirect('user/index');
        }

    }

    //custom validation function for dropdown input
    function combo_check($str)
    {
        if ($str == '-SELECT-')
        {
            $this->form_validation->set_message('combo_check', 'Invalid %s you need pick one');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

    //custom validation function to accept only alpha and space input
    function alpha_only_space($str)
    {
        if (!preg_match("/^([-a-z ])+$/i", $str))
        {
            $this->form_validation->set_message('alpha_only_space', 'The %s field must contain only alphabets or spaces');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}
?>

观看次数:

userview2.php

<section id="signup">
        <div class="container2" >
      <!-- Example row of columns -->
      <div class="row">
        <?php 
        $attributes = array("class" => "form-horizontal","id" => "userform", "name" => "userform");
        echo form_open("user/index", $attributes);?>


            <legend>Sign up Now </legend>
            <div class="form-group">
            <div class="row colbox">


            <div class="col-lg-4 col-sm-4">
                <label for="name" class="control-label">Name</label>
            </div>
            <div class="col-lg-8 col-sm-8">
                <input id="name" name="name" placeholder="your name" type="text" class="form-control"  value="<?php echo set_value('name'); ?>" />
                <span class="text-danger"><?php echo form_error('name'); ?></span>
            </div>
            </div>
            </div>


            <div class="form-group">
            <div class="row colbox">
            <div class="col-lg-4 col-sm-4">
                <label for="email" class="control-label">Email address</label>
            </div>
            <div class="col-lg-8 col-sm-8">
                <input id="email" name="email" placeholder="type your email here" type="text" class="form-control"  value="<?php echo set_value('email'); ?>" />
                <span class="text-danger"><?php echo form_error('email'); ?></span>
            </div>
            </div>
            </div>


            <div class="form-group">
            <div class="row colbox">
            <div class="col-lg-4 col-sm-4">
                <label for="passconf" class="control-label">Password</label>
            </div>
            <div class="col-lg-8 col-sm-8">
                <input id="passconf" name="passconf" placeholder="your password" type="password" class="form-control" value="<?php echo set_value('passconf'); ?>" />
                <span class="text-danger"><?php echo form_error('passconf'); ?></span>
            </div>
            </div>
            </div>

            <div class="form-group">
            <div class="row colbox">
            <div class="col-lg-4 col-sm-4">
                <label for="country" class="control-label">Country</label>
            </div>
            <div class="col-lg-8 col-sm-8">

                <?php
                $attributes = 'class = "form-control" id = "country"';
                echo form_dropdown('country',$country,set_value('country'),$attributes);?>
                <span class="text-danger"><?php echo form_error('country'); ?></span>
            </div>
            </div>
            </div>


            <div class="form-group">
            <div class="col-sm-offset-4 col-lg-8 col-sm-8 text-left">
                <input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Insert" />
                <input id="btn_cancel" name="btn_cancel" type="reset" class="btn btn-danger" value="Cancel" />
            </div>
            </div>

        <?php echo form_close(); ?>
        <?php echo $this->session->flashdata('msg'); ?>


      </div>


    </section>

【问题讨论】:

  • 是否有任何错误或警告信息?
  • 你好,这个消息看起来很好,我只是不想刷新页面得到错误消息,明白吗?像ajax

标签: javascript php jquery codeigniter validation


【解决方案1】:

使用 JSON Ajax:

控制器:

 private $_message = array();

 if ($this->form_validation->run() == FALSE):
$this->_message = array('type' => 'error', 'message' => validation_errors());
 else:
 $this->_message = array('type' => 'success', 'message' => 'Success!');
                    endif;

JS

 if (response.type === 'success') {
 console.log(response.message);
}

【讨论】:

  • 感谢您的帮助,我在代码中插入时出错了,您能告诉我您应该将它放在我的控制器中的哪个位置吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-29
  • 2012-07-26
  • 2015-07-31
  • 1970-01-01
  • 2013-03-17
  • 2018-01-28
相关资源
最近更新 更多