0

I am creating a status update system where i need to upload a image as well as show it when it uploads all using ajax but when i send the image it goes to the database but i cannot access the image in the ajax return

here is the code

 <div class="tab-pane fade" id="tabs-2">
    <?php echo form_open_multipart('',["id"=>"formupload","name"=>"formupload"]); ?>
    <p class="formUnit"> <i class="active_pic"></i>

    <input type="hidden" name="id" value="<?php echo $id; ?>">    
    <button class="uibutton" type="button" id="upload_pic" style="width: 230px; height: 150px;">Upload Picture</button><span id="status"></span>
    <?php echo form_upload(["name"=>"imagefile","id"=>"upload_pic"  ]); ?>
    <ol class="controls clearfix">
    <?php echo form_submit(['name'=>'submit','value'=>'Submit',"class"=>"btn btn-primary"]); ?>
    </ol>
    </p>
    <p id="files"></p>
    <?php echo form_close(); ?>
</div>

now ajax

 jQuery('#formupload').submit(function(e){

        e.preventDefault();
        var formData = new FormData(this);
        var url= '<?php echo base_url("user/postData_image"); ?>';
        formData.value

        jQuery.ajax({

            type: "POST",
            url:url,
            data: formData,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data)
            {
                console.log(data);
                $('#output_post').attr('src',data);
      },
            error: function(data){
            //error function
          }
       });            
    });

now controller

public function postData_image()
  {
      $config = [
                  'upload_path' =>    './uploads/',
                  'allowed_types' =>    'jpg|gif|png|jpeg',
                  'max_size'            => 10000000000000,
                  'max_width'            => 1024000000,
                  'max_height'           => 7680000000,
                ];
              $this->load->library('upload', $config);
              $this->upload->initialize($config); 
              $imagefile="imagefile";
        if(!$this->upload->do_upload($imagefile)) 
         {
          $upload_error = $this->upload->display_errors();
          $this->load->view('dashboard/profile',compact('upload_error'));
         }
        else 
         {
          $post = $this->input->post();
          //print_r($post);
          unset($post['submit']);
          $upload_data = $this->upload->data();
          $file_name=$_FILES['imagefile'];
          $this->load->model('Pmodel');
          $post_data=$this->Pmodel->post_data_image($post,$file_name);
          $post['data']=$post_data;

          echo $image_path= base_url("uploads/".$upload_data['raw_name'].$upload_data['file_ext']);
          return $post;

         }

  }

model

public function post_data_image($arr,$arra)
{

    $id=$arr['id'];
    $image=$arra['name'];

$data=array('image'=>$image);


    $query=$this->db->insert('post_status',['user_id'=>$id,'image'=>$image]);
    return $query;
}

but how to return the value that is generated after insert in the database using ajax

2 Answers 2

2

You want to output your post as json so jquery can interpret it.

  echo json_encode($post);

To your ajax function add:

dataType: 'json'

And then data will be an array you can use.

Sign up to request clarification or add additional context in comments.

4 Comments

if i echo json_encode($post) it shows this in the response {"id":"1","data":true}
but how can i access that data in my view using foreach?
sorry still wont let the data pass
0

Thanx to @Callombert i got the answer for what i was looking i wanted to return the value and 'echo json_encode('$image_path) or $post would return the value in the json form thus you cacn access it in your view

for someone else looking for an answer just add echo json_encode($image_path);

To your ajax function add:

dataType: 'json'

this would get you working.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.