我没有在codeigniter中输入表单中的任何文本值

时间:2017-07-28 19:31:43

标签: php html image codeigniter

我对Codeigniter很新。我试图创建一个带有一些文本输入字段的表单以及两个图像上传字段。图像上传工作正常但文本输入字段值不到。任何人都可以检查我的代码并告诉我我在哪里做错了这是我的代码:

前端

    <body>
        <div class="custom-container">
        <div id="msg"></div>
            <form  id="product-upload" action="/index.php/uploadproduct/upload" method="POST" accept-charset="utf-8"  enctype="multipart/form-data"">
                <div class="form-group">
                    <label for="product-name">Product name</label>
                    <input type="text" name="product_name" class="form-control">
                </div>
                <div class="form-group">
                    <label for="product-name">Product Code</label>
                    <input type="text" name="product_code" class="form-control">
                </div>
                <div class="form-group">
                    <label for="product-name">Product Link</label>
                    <input type="text" name="product_link" class="form-control">
                </div>
                <div class="form-group">
                    <label for="product-image">Product image</label>
                    <input type="file" id="product-image" name="product_image" class="form-control">
                </div>
                <div class="form-group">
                    <label for="product-name">Product Screenshots</label>
                    <input type="file" id="product-screen" name="product_screen" class="form-control" multiple>
                </div>
                <div class="form-group">
                    <input id="add-product" type="Submit" class="btn btn-primary" value="Add new product">
                </div>
            </form>
        </div>
    </body>

     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

     <script type="text/javascript">
        $(document).ready(function(){
            $('#add-product').click(function(e){
                e.preventDefault();
                var formData = new FormData();

                //for product profile images
                var productProfile = $('#product-image').prop('files')[0];
                formData.append('file',productProfile);

                // for product detail image
                var imageCount = document.getElementById('product-screen').files.length;
                for (var i = 0; i< imageCount; i++) {
                    formData.append("files[]", document.getElementById('product-screen').files[i]);
                }


                //AJAX Call
                $.ajax({
                    url: 'http://localhost/ci/index.php/uploadproduct/upload/', // point to server-side controller method
                    dataType: 'text', // what to expect back from the server
                    cache: false,
                    contentType: false,
                    processData: false,
                    data: formData,
                    type: 'post',
                     beforeSend: function() {
                        // setting a timeout
                         $('#msg').html('Loading');
                    },
                    success: function (response) {
                        $('#msg').html(response); // display success response from the server
                        $('input').attr('value').html();
                    },
                    error: function (response) {
                        $('#msg').html("no response"); // display error response from the server
                    }
                });
            });
        });

     </script>

控制器脚本就是这个

       public function upload(){
            $uploadData = "";
            //Get the details 
            $productName = $_POST['product_name'];
            $productCode = $this->input->post('product_code');
            $productLink = $this->input->post('product_link');
            $uploadData = $productName.','.$productCode.','.$productLink;

            // setting cofig for image upload
            $config['upload_path'] = 'uploads/profile/';
            $config['allowed_types'] = '*';
            $config['max_filename'] = '255';
            $config['encrypt_name'] = TRUE;
            //$config['max_size'] = '1024'; //1 MB

            // Get the profile image
            $errorMsg = "";
            if (isset($_FILES['file']['name'])) {
                if (0 < $_FILES['file']['error']) {
                    $errorMsg = 'Error during file upload' . $_FILES['file']['error'];
                } else {
                    if (file_exists('uploads/profile/' . $_FILES['file']['name'])) {
                        $errorMsg =  'File already exists : uploads/profile/' . $_FILES['file']['name'];
                    } else {
                        $this->load->library('upload', $config);
                        if (!$this->upload->do_upload('file')) {
                            $errorMsg =  $this->upload->display_errors();
                        } else {
                             $data = $this->upload->data();

                           $errorMsg =  'File successfully uploaded : uploads/profile/' . $_FILES['file']['name'];
                           $uploadData = $uploadData.','.$data['full_path'];
                        }
                    }
                }
            } else {
                $errorMsg =  'Please choose a file';
            }


            //upload product screenshots
            $config['upload_path'] = 'uploads/';
            if (isset($_FILES['files']) && !empty($_FILES['files'])) {
                $no_files = count($_FILES["files"]['name']);
                $link="";
                for ($i = 0; $i < $no_files; $i++) {
                    if ($_FILES["files"]["error"][$i] > 0) {
                        $errorMsg =  "Error: " . $_FILES["files"]["error"][$i] . "<br>";
                    } else {
                        if (file_exists('uploads/' . $_FILES["files"]["name"][$i])) {
                            $errorMsg =  'File already exists : uploads/' . $_FILES["files"]["name"][$i];
                        } else {
                            $fileOriginalNmame = $_FILES["files"]["name"][$i];
                            $explodeFile = explode(".",$fileOriginalNmame);
                            $fileExtenstion = end($explodeFile);
                            $fileName = md5(md5(uniqid(rand(), true)).$_FILES["files"]["name"][$i]).'.'.$fileExtenstion;
                            move_uploaded_file($_FILES["files"]["tmp_name"][$i], 'uploads/' . $fileName);

                            $link= $link.$fileName.',';



                        }
                    }

                }
                $uploadData =$uploadData .','.  $link;
                $errorMsg = $uploadData;
            } else {
                $errorMsg =  'Please choose at least one file';
            }

            echo $errorMsg;

        }

如果有人可以改进我的控制器代码,这将非常有用tnx。

1 个答案:

答案 0 :(得分:1)

  

FormData()方法:

根据我们的定义.FormData()Key/Value形式提交元素数据。 Form元素必须具有name属性。 FormData()的一个优点是,您现在可以在下一页上发布文件。

简单语法:

var formData = new FormData(form);

突出显示点数:

  1. 此方法会发布文件。

  2. 此方法使用Get&amp; amp;发布方法包括文件。

    var formData = new FormData();

    formData.append(&#39;用户名&#39;,&#39; joe&#39;);

  3. 此外,您可以使用FormData.append为此添加键/值对。

  4. 所以你的代码坏了,因为你需要将输入值作为你错过的密钥/对格式传递,除了文件。

    希望这会对你有所帮助。

      

    请在下面找到解决方案。

    &#13;
    &#13;
     $(document).ready(function(){
                $('#add-product').click(function(e){
                    e.preventDefault();
                    var formData = new FormData();
    
                    //for product profile images
                    var productProfile = $('#product-image').prop('files')[0];
                    formData.append('file',productProfile);
    
                    // for product detail image
                    var imageCount = document.getElementById('product-screen').files.length;
                    for (var i = 0; i< imageCount; i++) {
                        formData.append("files[]", document.getElementById('product-screen').files[i]);
                    }
                    var inputs = $('#product-upload input[type="text"],input[type="email"]');
                    $.each(inputs, function(obj, v) {
                        var name = $(v).attr("name");
                        var value = $(v).val();
                        formData.append(name, value);
                    });
                    
    
                    //AJAX Call
                    $.ajax({
                        url: 'http://localhost/ci/index.php/uploadproduct/upload/', // point to server-side controller method
                        dataType: 'text', // what to expect back from the server
                        cache: false,
                        contentType: false,
                        processData: false,
                        data: formData,
                        type: 'post',
                         beforeSend: function() {
                            // setting a timeout
                             $('#msg').html('Loading');
                        },
                        success: function (response) {
                            $('#msg').html(response); // display success response from the server
                            $('input').attr('value').html();
                        },
                        error: function (response) {
                            $('#msg').html("no response"); // display error response from the server
                        }
                    });
                });
            });
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="custom-container">
            <div id="msg"></div>
                <form  id="product-upload" action="/index.php/uploadproduct/upload" method="POST" accept-charset="utf-8"  enctype="multipart/form-data">
                    <div class="form-group">
                        <label for="product-name">Product name</label>
                        <input type="text" name="product_name" class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="product-name">Product Code</label>
                        <input type="text" name="product_code" class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="product-name">Product Link</label>
                        <input type="text" name="product_link" class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="product-image">Product image</label>
                        <input type="file" id="product-image" name="product_image" class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="product-name">Product Screenshots</label>
                        <input type="file" id="product-screen" name="product_screen" class="form-control" multiple>
                    </div>
                    <div class="form-group">
                        <input id="add-product" type="Submit" class="btn btn-primary" value="Add new product">
                    </div>
                </form>
            </div>
    &#13;
    &#13;
    &#13;

    如果它不适合你,请告诉我。