CodeIgniter表单验证whitout重新加载页面

时间:2015-06-22 15:34:25

标签: javascript php jquery codeigniter validation

我在CodeIgniter中有一个完美的表单,我只是遇到一个小问题,我无法在不重新加载页面的情况下显示验证,是否有一种简单的方法可以解决这个问题?

控制器:

user.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>

1 个答案:

答案 0 :(得分:0)

使用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);
}
相关问题