Codeigniter:从表单上传无效

时间:2016-05-04 14:51:38

标签: php codeigniter

我将图像上传到“images”目录,并将一些信息上传到codeigniter中的数据库。但是当我在完成表单后点击提交按钮时,它什么也没做。它只是刷新。如果有人可以请查看此代码,并告诉我为什么它不起作用。先感谢您。 这是我的控制器代码:

<?php       public function create()
    {

        if (!isset($_POST['submit']))
        {
            $title['title'] = 'Add News';
            $this->load->view('templates/navbar', $title);
            $this->load->view('News/create');

        }
        else
        {
            // image1
            $ImageName = $_FILES['image']['name'];
            $imageSize= $_FILES['image']['size'];
            $imageSource = $_FILES['image']['tmp_name']; 
            move_uploaded_file($_FILES['image']['tmp_name'], "images/$ImageName");
            $this->create_thumb($ImageName);
            $this->compress($ImageName, $imageSize, "images/$ImageName");

            // image2
            if ($this->input->post('image2')){
            $ImageName2 = $_FILES['image2']['name'];
            $imageSize2= $_FILES['image2']['size'];
            $imageSource2 = $_FILES['image2']['tmp_name']; 
            $this->compress($ImageName2, $imageSize2, $imageSource2);
            }
            // image3
            if ($this->input->post('image3')){
            $ImageName3 = $_FILES['image3']['name'];
            $imageSize3= $_FILES['image3']['size'];
            $imageSource3 = $_FILES['image3']['tmp_name']; 
            $this->compress($ImageName3, $imageSize3, $imageSource3);
            }
            // image4
            if ($this->input->post('image4')){
            $ImageName4 = $_FILES['image4']['name'];
            $imageSize4= $_FILES['image4']['size'];
            $imageSource4 = $_FILES['image4']['tmp_name']; 
            $this->compress($ImageName4, $imageSize4, $imageSource4);
            }

            $this->news_model->set_news();
            $this->load->view('News/success');
        }
    }

    ?>

这是我的观看代码:

       <div class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="content">
                            <form action="<?php echo base_url();?>index.php/news/create/" enctype="multipart/form-data" method="post" accept-charset="utf-8">
                                <div class="row">
                                    <div class="col-md-8">
                                        <div class="form-group">
                                            <label>Сарлавха:</label>
                                            <input type="text" name="title" class="form-control" placeholder="Сарлавха">
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Автомобил тури:</label>
                                            <select name="category" class="form-control">
                                                    <option value="yuk">Юк</option>
                                                    <option value="kichik">Кичик тижорат</option>       
                                                    <option value="yengil">Енгил</option>
                                                    <option value="maxsus">Махсус техника</option>
                                                    <option value="tyagach">Тягач</option>
                                                    <option value="prisep">Прицеп</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Нархи:</label>
                                            <input type="number" name="price" class="form-control" placeholder="Нархи">
                                        </div>
                                    </div>
                                    <div class="col-md-8">
                                        <div class="form-group">
                                            <label>Исми ва шарифи:</label>
                                            <input type="text" name="name" class="form-control" placeholder="Исми ва шарифи">
                                        </div>
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Телефон рақам:</label>
                                            <input type="text" name="telno" class="form-control" value="+998" placeholder="Телефон рақам">
                                        </div>
                                    </div>
                                    <div class="col-md-8">
                                        <div class="form-group">
                                            <label>Жойи:</label>
                                            <select name="location" class="form-control">
                                                    <option value="Қорақалпоғистон Республикаси">Қорақалпоғистон Республикаси</option>
                                                    <option value="Бухоро вилояти">Бухоро вилояти</option>
                                                    <option value="Жиззах вилояти">Жиззах вилояти</option>      
                                                    <option value="Қашқадарё вилояти">Қашқадарё вилояти</option>
                                                    <option value="Навоий вилояти">Навоий вилояти</option>
                                                    <option value="Наманган вилояти">Наманган вилояти</option>
                                                    <option value="Сурхондарё вилояти">Сурхондарё вилояти</option>
                                                    <option value="Сирдарё вилояти">Сирдарё вилояти</option>
                                                    <option value="Тошкент вилояти">Тошкент вилояти</option>
                                                    <option value="Фарғона вилояти">Фарғона вилояти</option>
                                                    <option value="Хоразм вилояти">Хоразм вилояти</option>
                                                    <option value="Тошкент шаҳар">Тошкент шаҳар</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label>Қўшимча маълумот:</label>
                                            <textarea rows="5" name="info" class="form-control" placeholder="Қўшимча маълумот" ></textarea>
                                        </div>
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Асосий расмни танланг:</label>
                                            <input type="file" name='image' class="form-control" accept="image/*" >
                                        </div>
                                    </div>
                                                                            <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Қўшимча расмни танланг:</label>
                                            <input type="file" name='image2' class="form-control" accept="image/*" >
                                        </div>
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Қўшимча расмни танланг:</label>
                                            <input type="file" name='image3' class="form-control" accept="image/*" >
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Қўшимча расмни танланг:</label>
                                            <input type="file" name='image4' class="form-control" accept="image/*" >
                                        </div>
                                    </div>
                                </div>

                                <button type="submit" class="btn btn-info btn-fill pull-right" name="submit" value="submit">Юклаш</button>
                                <div class="clearfix" ></div>
                            </form>
                        </div>
                    </div>
                </div>


            </div>
        </div>
    </div>

1 个答案:

答案 0 :(得分:0)

如果您要发布提交,则必须在视图文件中使用<input />代替<button />

但如果发布了一些内容,还有更好的方法可以检查:

if (!isset($_POST['submit']))
{
    // do something if NOT posted
}
else
{
   // do something if posted
}

更改为:

if ($this->input->post())
{
    // do something if posted
}
else
{
   // do something if NOT!! posted
}