繁体   English   中英

使用CodeIgniter中的Ajax将表单数据与输入类型文件一起发送

[英]Sending form data along with input type file using ajax in codeIgniter

我有一个简单的表单,其中包括输入类型文件和其他表单字段。 我能够将输入类型文件数据发送到控制器,但是将其他表单数据与输入类型文件一起发送存在问题。
到目前为止,这是我所做的-
表格-

<form method="post" id="theme_option_form" enctype="multipart-formdata">
        Logo :<input type="file" name="image_file" id="image_file">
        Logo Size :<input type="text" id="logo_size" class="form-control input-sm">
        Heading Size :<input type="text" id="hding_size" class="form-control input-sm">
        <input type="submit" name="upload" id="upload" value="Upload" class="btn btn-primary btn-sm">
</form>  

Ajax代码-

$('#theme_option_form').on("submit",function(e){
    var hding_size=$('#hding_size').val();   
    var logo_size=$('#logo_size').val();
                $.ajax({
                    url:base_url+'admin/theme_options_data',
                    method:'post',
                    data:new FormData(this),
                    contentType:false,
                    cache:false,
                    processData:false,
                    success:function(data){
                       alert(data);
                    }
                });         
});  

控制器功能-

public function theme_options_data()
 {
       if(isset($_FILES['image_file']['name']))
        {
          $this->load->helper('string');
          $logo_name=random_string('alnum',5);
          $config['upload_path']='./upload';
          $config['allowed_types']='jpg|jpeg|png|gif';
          $config['file_name']=$logo_name;
          $this->load->library('upload',$config);
          if(!$this->upload->do_upload('image_file'))
            {
              echo $this->upload->display_errors();
            }
            else
            {
              $data=$this->upload->data();
            }
          }

    }  

文件上传正常工作,但是我只需要将表单数据(徽标大小,标题大小等)发送到控制器,然后我必须将所有数据存储到数据库中。
请帮我。
谢谢

表格 ...输入字段名称。

<form method="post" id="theme_option_form" enctype="multipart-formdata">
    Logo :<input type="file" name="image_file" id="image_file">
    Logo Size :<input type="text" name="logo_size" id="logo_size" class="form-control input-sm">
    Heading Size :<input type="text" name="hding_size" id="hding_size" class="form-control input-sm">
    <input type="submit" name="upload" id="upload" value="Upload" class="btn btn-primary btn-sm">

控制者

public function theme_options_data()
 {
       if(isset($_FILES['image_file']['name']))
        {
          $this->load->helper('string');
          $logo_name=random_string('alnum',5);
          $config['upload_path']='./upload';
          $config['allowed_types']='jpg|jpeg|png|gif';
          $config['file_name']=$logo_name;
          $this->load->library('upload',$config);
          if(!$this->upload->do_upload('image_file'))
            {
              echo $this->upload->display_errors();
            }
            else
            {
              $data=$this->upload->data();
            }
          }
       $logo_size = $_POST['logo_size'];
       $heading_size  = $_POST['hding_size'];
    }  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM