doukun8670 2016-10-12 17:49
浏览 70
已采纳

PHP - 使用json进行Codeigniter验证

I have a fields and every field have a validation. all validation are working.

But the problem is in my field that will upload file. Even though i already put an image file still it will validate that I must upload an image.

im using sweetalert to show the validation errors.

MY CONTROLLER

public function saveproducts(){
    $config['upload_path'] = 'uploads/products';   
    $config['allowed_types'] = 'jpg|png|jpeg';
    $config['max_size'] = '2048000';
    $config['overwrite'] = TRUE;
    //$config['file_name'] = $this->input->post('prod_name');

    $this->load->library('upload', $config);

    $this->form_validation->set_rules('userfile', 'Product Image','callback_rulesprodimage');

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

        $products = array(
            'product_image' => $this->input->post('userfile'),
            'upload_data' => $this->upload->data(),
            );

        $this->CrudModel->insertproductdata($products);

        echo json_encode(1);
    }

}

public function rulesprodimage(){
    if (isset($_FILES['userfile']) && !empty($_FILES['userfile']['name'])){
        if ($this->upload->do_upload('userfile')){
        return true;
        }else{
        $this->form_validation->set_message('rulesprodimage', $this->upload->display_errors());
        return false;
        }
    }else{
      // throw an error because nothing was uploaded
      $this->form_validation->set_message('rulesprodimage', "You must upload an image!");
      return false;
    }
}

VIEW

<form method="post" enctype="multipart/form-data" id="prod-submit">
<div class="form-group">
<label class="control-label">Product Image <span id="required"> * </span></label>
<input type="file" name="userfile" class="form-control">
</div>
</form>

Javascript

$(document).ready(function(){
$('#prod-submit').on('submit',function(e) { 

$.ajax({
    url: base_url+'adminpage/saveproducts/',

    data:$(this).serialize(),
    type:'POST',
    success:function(data){

      var result = JSON.parse(data); 
        if(result===1){ 
            console.log(result);
            document.location.href = base_url+"adminpage/products/"
        }
        else{
           swal({
                type: 'error',
                html: result,

              }).done();
        }
    },
    error:function(data){
    swal("Oops...", "Something went wrong :(", "error");

    }
  });
  e.preventDefault(); 
});

});

  • 写回答

2条回答 默认 最新

  • dtzd65908 2016-10-12 19:08
    关注

    The file doesn't go through ajax just like that, so $_FILES is empty and CI doesn't see. You can transport file without page reloading with FormData object. Read more on this question. Watch out also on browser compatibility, because this api works on modern browsers and ie10+.

    I usually do this with jquery plugins. There are many. One of the most popular is https://github.com/blueimp/jQuery-File-Upload , other for example: http://malsup.com/jquery/form/#file-upload

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

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题