CodeIgniter - Image submit button not working

后端 未结 3 954
时光取名叫无心
时光取名叫无心 2021-01-23 15:06

I am using CI to create a project for a client, I have a submit button as an image but it doesn\'t seem to be submitting the form, the code I have at the moment is.



        
相关标签:
3条回答
  • 2021-01-23 15:31
    **inside the view(as comerciales in my case)**, 
    
    
    
        <form action= "<?php echo site_url()?>/admin/doupload"  method="post" enctype="multipart/form-data" >
      <b>  Select the image to upload( Maximum size 500 kb, jpg, jpeg): </b>
        <input   style="color:#00A76F" type="file" name="fileToUpload" id="fileToUpload">
       <div  class="input-group" style="left:10%;width:85%;">
    
     <input class="btn btn-success pull-right" style="background:#00A76F" type="submit" value="Upload Image" name="submit">
    
    
    
    </div>
    
    
    </form>
       <div class="modal-body">
                        <div id="user_data">
                          <?php
                          if (!empty($massage)){
                              echo $massage ;
    
                          }
                            ?>
                        </div>
    
    **inside the controller define a method**
    
        public function doupload(){
           $root1 = $_SERVER['DOCUMENT_ROOT'];;
    
            $target_dir =  $root1."/nawaloka/uploads/";
    
                 // $target_dir =  $root1."/nawaloka/application/";
              $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    
    // Check if file already exists
    // Check file size
    if ($_FILES["fileToUpload"]["size"] > 500000) {
       $data['massage']= "Sorry, your file is too large.";
                $partials = array('content' => 'Admin/comerciales');
                $this->template->load('template/admin_template', $partials,$data);
    
        $uploadOk = 0;
    }
    if (is_dir($target_dir) && is_writable($target_dir)) {
    
        // do upload logic here
    } else {
        echo 'Upload directory is not writable, or does not exist.';
    }
    
    
    
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "jpeg" ) {
         $data['massage']= "Sorry, only JPG, JPEG files are allowed.";
                $partials = array('content' => 'Admin/comerciales');
                $this->template->load('template/admin_template', $partials,$data);
    
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
       $data['massage']= "Sorry, your file was not uploaded.";
                $partials = array('content' => 'Admin/comerciales');
                $this->template->load('template/admin_template', $partials,$data);
    
    // if everything is ok, try to upload file
    } else {
                array_map('unlink', glob("/var/www/html/nawaloka/uploads/*"));
    
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],"/var/www/html/nawaloka/uploads/shamith.jpg")) {
               $data['massage']= "The image ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
                $partials = array('content' => 'Admin/comerciales');
                $this->template->load('template/admin_template', $partials,$data);
    
    
            //echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        } else {
    
                 $data['massage']= "Sorry, there was an error while uploading your file.";
                $partials = array('content' => 'Admin/comerciales');
                $this->template->load('template/admin_template', $partials,$data);
        }
    }
    
    
    
    
    }
    
    0 讨论(0)
  • 2021-01-23 15:32

    Is it across all browsers? Or IE specific? What about if you add an onclick event just for testing? onclick="javascript:document.formidhere.submit()"

    I'd recommend using either <input type="submit" /> or my preference would be to use <button type="submit> due to browser inconsistencies with <input type="image" /> and just style the button with something like:

    button{
        background:url('/images/subscribe_free.jpg') no-repeat;
        height:29px;
        width:207px;
        border:0;
    }
    
    0 讨论(0)
  • 2021-01-23 15:39

    Try adding value="trialSubmit" to get the image to submit. Seemed to work for me.

    You could also see if this answer helps: image submit button

    0 讨论(0)
提交回复
热议问题