doupeng3501 2016-04-29 09:31
浏览 85

Codeigniter Ajax对插入函数的验证

I have this form where i can insert data, on code igniter. it has validations, all i want to do is to make it ajax. how can I put ajax on it?

my codes,

controller

// CREATE /////////////////////////////////////////////////////////

public function create(){

    $this->form_validation->set_rules('PROVINCE','Province Name','trim|required|max_length[30]|callback_if_exist');

    if($this->form_validation->run($this) == FALSE){

        $this->add_view();
    }else{

        if($query = $this->Provinces_Model->insert()){
            redirect('Provinces/index');
        }else{
            $this->add_view();
        }

    }

}

/////////// Checking duplicates

public function if_exist(){

    $available = $this->Provinces_Model->check_if_exist(); //change

    if($available){
        return TRUE;
    }else{

        $this->form_validation->set_message('if_exist','{field} already exist!');
        return FALSE;

    }

}

models

/// call back

public function check_if_exist(){

    $sql = "SELECT * FROM $this->table WHERE PROVINCE = ?";
    $data = array('PROVINCE' => $this->input->post('PROVINCE'));

    $query = $this->db->query($sql, $data);

    if($query->num_rows() == 0){

        return TRUE;
    }else{

        return FALSE;
    }

}

///// CREATE /////////////////////////////////////////////////////////

public function insert(){

    $input = array(
            'PROVINCE' => $this->input->post('PROVINCE')
            );
    $insert = $this->db->insert($this->table,$input);
    return $insert;
}

and finally the view

            <div class="box-body">
              <div class="form-group">
                <label for="PROVINCE" class="col-sm-2 control-label col-sm-offset-2">Province Name:</label>
                  <div class="col-sm-5">
                    <input type="text" class="form-control" id="PROVINCE" name="PROVINCE" value = "<?= set_value("PROVINCE"); ?>">
                  </div>
              </div>
              <div class="form-group has-error col-sm-offset-7">
                <label class="control-label" for="error"><?php echo form_error("PROVINCE"); ?></label>
              </div>
            </div>

notice that I have the error reporting so and value = "<?= set_value("PROVINCE"); ?>" for retaining the value i inserted if its values did not pass the validation.. now I want to make it ajax as a practice because the next part that I am going to do is after I submitted a form, it will go to another form.

  • 写回答

1条回答 默认 最新

  • dongxian1699 2016-04-29 09:50
    关注

    Edit your code like below:

    Controller:

    public function create(){
    
        $this->form_validation->set_rules('PROVINCE','Province Name','trim|required|max_length[30]|callback_if_exist');
    
        if($this->form_validation->run() == FALSE){
    
            echo 'ERR';
        }else{
    
            if($query = $this->Provinces_Model->insert()){
                redirect('Provinces/index');
            }else{
                $this->add_view();
            }
    
        }
    
    }
    

    And the view's Javascript part:

        <script type="text/javascript" >
        $('#send').click(function(){ // you should add an id called "send" to your submit button
       var province = $('#province').val();
    
            $.ajax({ 
                type:'POST',
                url:'yourposturl', 
                data: province,
                success:function(response){
                  if (response = 'ERR') {
                    $("#response").html('Some error text');
                     }
                    else { 
                    $("#response").html('Some success text');   
                    }
                }
            });
          });
        </script>
    

    Also please change <label class="control-label" for="error"> part to <label class="control-label" id="response" for="error"> if you want to use this method.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度