我没有在codeigniter中的表单输入中获取任何文本值 [英] I am not getting any text value in form input in codeigniter

查看:148
本文介绍了我没有在codeigniter中的表单输入中获取任何文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am very new to Codeigniter. I m trying to create a form with some text input field along with two image upload field. The image uploading working fine but the text input field value are not coming. Can anyone please check my code and tell me where I am doing wrong Here is my Code:

前端

    <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。

And if anyone can improve my controller code that will be very helpful tnx.

推荐答案


FormData()方法:

根据我们的定义 .FormData()在<$ c中提交元素数据$ c>键/值表单。 Form元素必须具有name属性。 FormData()的一个优点是,您现在可以在下一页上发布文件。

As per our definition .FormData() submit a element data in a Key/Value form. The Form element must have a name attribute. One advantage of FormData() is now you can post a files on next page.

简单语法:

var formData = new FormData(form);

精彩积分:


  1. 此方法会发布文件。

  1. This method does post files.

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

This method post complete form using Get & Post method including files.

var formData = new FormData();

var formData = new FormData();

formData.append('username' ,'joe');

formData.append('username', 'joe');

此外,您可以使用 FormData.append

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

So your code broke because you need to pass value of input as key/pair format that you missed except for file.

希望这会对你有帮助。


请找到下面的解决方案。

 $(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
                    }
                });
            });
        });

<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>

让我知道它是否适用于你。

Let me know if it not works for you.

这篇关于我没有在codeigniter中的表单输入中获取任何文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆