duan0802 2015-04-23 18:13
浏览 9
已采纳

来自codeigniter的模态上的错误消息

I've made a form on a modal dialog to insert into a database

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Crear nueva clase</h4>
                  </div>
                  <div class="modal-body">
                    <?php echo validation_errors(); ?>
                    <?php echo form_open('clase/create');?>  
                    <form class="form col-md-12 center-block">
                          <div class="form-group">
                            <input type="text" class="form-control input-lg"  id="name" name="name" placeholder="Nombre"/>
                          </div>
                          <div class="form-group">
                             <input type="text" class="form-control input-lg" id="info" name="info" placeholder="Información"/>
                          </div>
                          <div class="form-group">
                            <button typ="submit"class="btn btn-success btn-lg btn-block">Crear</button>
                          </div>
                        </form>

                  </div>
                </div>
              </div>
            </div>

Works but the error messages when the validation is not passed are not shown in nowhere

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

class Clase extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('clase_model','',TRUE);
   $this->load->helper(array('form'));
 }

 function index()
 {
    if($this->session->userdata('logged_in')){
        $data['title'] = 'Gestión de Clases';
        $data['clases'] = $this->clase_model->getAll();
      $this->load->view('header', $data);
      $this->load->view('clase_view', $data);

     }
     else{

          redirect('login', 'refresh');
     }
 }

 function create(){
    if($this->session->userdata('logged_in')){
      $this->load->library('form_validation');
      $this->form_validation->set_rules('name', 'Nombre', 'trim|min_length[2]|required');
      $this->form_validation->set_rules('info', 'Información', 'trim');

       if($this->form_validation->run() == FALSE){
          $data['clases'] = $this->clase_model->getAll();
          $this->load->view('header', $data);
          $this->load->view('clase_view', $data);
       }
       else{
          if($this->input->post('info')){
             $this->insert2($this->input->post('name'),$this->input->post('info'));
          }
          else{
            $this->insert1($this->input->post('name')); 
          }
       }   
    }
    else{
          redirect('login', 'refresh');
    }
 }

 function insert2($name,$information){
    $dat = array(
      'nombre'=>$name,
      'info'=>$information
    );
    $this-> db ->insert('clase',$dat);

    echo $name;
    redirect('clase', 'refresh');
 }
 function insert1($name){
    $dat = array(
      'nombre'=>$name,
    );

    $this-> db ->insert('clase',$dat);
    redirect('clase', 'refresh');
 }
}
?>

The create method works correct in any case but I need to see the valifdation error messages,how I can do it??

  • 写回答

1条回答 默认 最新

  • drci47425 2015-04-23 20:00
    关注

    If you want to show your error message on the modal then you should submit your form via ajax.And from controller function echo your error message using json_encode function. Such like-

        if ($this->form_validation->run() === FALSE) {
            $data = array('error_message' => form_error('input_name'));
            echo json_encode($data);
            }
    

    And using jQuery do this-

            $(document).on("click", ".btn", function () {
                 $.ajax({
                        type: "POST",
                        url: "your/url",
                        data: {your:data},
                        success: function(data) {
    
       //and from data parse your json data and show error message in the modal
                        var obj = $.parseJSON(data);
                            if(obj!=null)
                                {                             
                                    $('#err_mssg').html(obj['error_message']);
                                }
                        }});
                     });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?