dongluo1853 2016-03-29 13:47
浏览 49
已采纳

如何防止使用codeigniter提交表单上的bootstrap模式?

I have some issues about displaying alert message inside a bootstrap modal form if inputs doesn't meet the form validation rule. I'm stuck whether the form validates or not and I wanted to display the error inside my modal form, somewhere at the top.

Controller: Users.php

function create_user() {

 // set validation rules
  $this->form_validation->set_rules('first_name', 'Firstname', 'trim|required|min_length[4]');
  $this->form_validation->set_rules('last_name', 'Lastname', 'trim|required');

  // set validation errors
  $this->form_validation->set_message('required', '%s field is required');
  $this->form_validation->set_message('min_length[4]', '%s min_length');

if ($this->form_validation->run() == FALSE) {
  echo "something";
} else {
  echo "something";
}


  $data = array(
    'first_name' => $this->input->post('first_name'),
    'last_name' => $this->input->post('last_name'),
    'phone' => $this->input->post('phone'),
    'email' => $this->input->post('email')
    );

  $output = $this->user_model->put_user($data);
  if (!$output === TRUE) {
    echo "data saved succesfully";
    // redirect('users', 'refresh');
  } else {
    redirect('users', 'refresh');
  }
}

View: userList.php

<div class="modal-body">
 <?php echo form_open("users/create_user", $attributes);?>
 <?php echo validation_errors(); ?>
 <div id="error" class="alert alert-info"></div>
 <div class='error_msg'></div>             
   <div class="form-group">
    <label for="first_name" class="col-sm-3 control-label">First Name</label>
      <div class="col-sm-9">
       <?php echo form_input(['id' => 'first_name', 'name' => 'first_name', 
       'class' => 'form-control', 'placeholder' => 'Firstname',
       'value' => set_value('first_name')]); ?>
      </div>
      </div>

      <div class="form-group">
       <label for="last_name" class="col-sm-3 control-label">Last Name</label>
       <div class="col-sm-9">
        <?php echo form_input(['id' => 'last_name', 'name' => 'last_name', 
        'class' => 'form-control', 'required' => 'required', 'placeholder' => 'Lastname', 'value' => $this->input->post('last_name')]); ?>
        </div>
       </div>

      <div class="form-group">
       <label for="phone" class="col-sm-3 control-label">Contact No.</label>
        <div class="col-sm-9">
         <?php echo form_input(['id' => 'phone', 'name' => 'phone', 
         'class' => 'form-control', 'required' => 'required', 'placeholder' => 'Phone', 'value' => $this->input->post('phone')]); ?>
        </div>
       </div>

       <div class="form-group">
        <label for="email" class="col-sm-3 control-label">Email</label>
         <div class="col-sm-9">
          <?php echo form_input(['id' => 'email', 'name' => 'email', 'type' => 'email', 'class' => 'form-control', 'required' => 'required', 'placeholder' => 'Email', 'value' => $this->input->post('email')]); ?>
         </div>
        </div>

       <div class="form-group">
       <label for="password" class="col-sm-3 control-label">Password</label>
        <div class="col-sm-9">
         <?php echo form_input(['id' => 'password', 'name' => 'firs_tname', 'type' => 'password', 'class' => 'form-control', 'required' => 'required', 'placeholder' => 'Password', 'value' => $this->input->post('password')]); ?>
        </div>
       </div>

       <div class="form-group">
        <div class="col-sm-offset-3 col-sm-9">
         <button type="submit" class="btn btn-default create-user">Create</button>
        </div>
       </div>   
     <?php echo form_close();?>
</div>

JQuery:

$(function() {

        $('#create_user').on('submit', function(event) {
            event.preventDefault();

            $.ajax({
                url: "<?php echo site_url('users/create_user') ?>",
                type: "POST",
                data: {"first_name" : <?php echo $this->input('first_name) ?>}, 
                dataType: "json",
                contentType: "application/json"
            }).done(function(msg) {
                $("#error").text(msg);
            });
        });
    };

I tried jquery prevent default but It seems not working for me or I have some mistakes writing the code. Any help would be much appreciated.

  • 写回答

2条回答 默认 最新

  • douju7503 2016-03-30 17:36
    关注

    It looks like your submit function is not executing. The default behavior of the form is executing and your jQuery code is ignored.

    Change the button so that it is no longer a submit button and give it an id.

    <button type="button" id="btn-submit" class="btn btn-default create-user">Create</button>
    

    Then change your method so that it triggers when you click the button.

    $(function() {
        $(document).on('click', '#btn-submit' function(event) {
            event.preventDefault();
    
            $.ajax({
                url: "<?php echo site_url('users/create_user') ?>",
                type: "POST",
                data: {"first_name" : <?php echo $this->input('first_name') ?>}, 
                dataType: "json",
                contentType: "application/json"
            }).done(function(msg) {
                $("#error").text(msg);
            });
        });
    };
    

    That should get you going in the right direction.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?