-1

Here is my HTML

<form class="form form-vertical captcha_from" style="width: 50%;" method="post" action="<?php echo base_url('home/post_job_save'); ?>" enctype = "multipart/form-data">
        <div class="container">

            <div class="row form-group">
                <div class="col-xs-12">
                    <ul class="nav nav-pills nav-justified thumbnail setup-panel">
                        <li class=""><a href="#step-1">
                            <h4 class="list-group-item-heading">Add Profile</h4>
                            <p class="list-group-item-text">First Step</p>
                        </a></li>
                        <li class=""><a href="#step-2">
                            <h4 class="list-group-item-heading">Add Resume</h4>
                            <p class="list-group-item-text">Second Step</p>
                        </a></li>
                        <li class=""><a href="#step-3">
                            <h4 class="list-group-item-heading">Add Cover Letter</h4>
                            <p class="list-group-item-text">Third Step</p>
                        </a></li>

                        <li class="active"><a href="#step-4">
                            <h4 class="list-group-item-heading">Add Photo</h4>
                            <p class="list-group-item-text">Fourth Step</p>
                        </a></li>

                    </ul>
                </div>
            </div>
            <div class="row setup-content" id="step-1" >
                <div class="col-xs-12">
                    <div class="col-md-12 well">
                        <h1>Add Profile</h1>

                            <div class="container">
                                <div class="row clearfix">
                                    <div class="col-md-12 column">
                                        <div class="control-group">
                  <label>First Name*</label>
                  <div class="controls">
                    <input type="text" class="form-control" placeholder="First Name" name="first_name" required="">
                  </div>
                </div>

                <div class="control-group">
                  <label>Last Name*</label>
                  <div class="controls">
                    <input type="text" class="form-control" placeholder="last Name" name="last_name" required="">
                  </div>
                </div>

                <div class="control-group">
                  <label>Email*</label>
                  <div class="controls">
                    <input type="email" class="form-control" placeholder="Email" name="email" required="">
                  </div>
                </div>
            </div>
        </div>
    </div>
                    </div>
                </div>
            </div>

    <div class="row setup-content" id="step-2">
        <div class="col-xs-12">
            <div class="col-md-12 well">
                <h1 class="text-center">Add Resume</h1>
                <div class="container">
                      <div class="row">
                        <div class="col-lg-12">
                            <div class="form-group">
                                <label for="file">Select a file to upload</label>

                                <input type="file" name="resume">
                                <p class="help-block">Only jpg,jpeg,png and gif file with maximum size of 1 MB is allowed.</p>
                            </div>
                        </div>
                      </div>
                </div><!-- /container -->

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

    <div class="row setup-content" id="step-3">
        <div class="col-xs-12">
            <div class="col-md-12 well">
                <h1 class="text-center">Add Cover Letter</h1>
                      <div class="form-group">
                        <label for="file">Select a file to upload</label>
                        <input type="file" name="cover_letter">
                        <p class="help-block">Only jpg,jpeg,png and gif file with maximum size of 1 MB is allowed.</p>
                      </div>

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

    <div class="row setup-content" id="step-4">
        <div class="col-xs-12">
            <div class="col-md-12 well">
                <h1 class="text-center">Add Photo</h1>
                      <div class="form-group">
                        <label for="file">Select a file to upload</label>
                        <input type="file" name="photo">
                        <p class="help-block">Only jpg,jpeg,png and gif file with maximum size of 1 MB is allowed.</p>
                      </div>
                      <input type="submit" id="apply_job_save_submit" class="btn btn-primary btn-md" value="Submit">
            </div>
        </div>

    </div>

</div></form>

Js Code

$('#apply_job_save_submit').on('click', function(e) {
    $.ajax({
        type: 'post',
        url: base_url+'jobs/home/apply_job_save',
        data: $('#apply_job_save').serialize(),
        success: function () {
          document.getElementById('createarea').innerHTML = "SUCCESS";
        }
      });
});

php code:

if(isset($_FILES) && $_FILES["resume"]["name"]!=''){
                $org_filename=$_FILES["resume"]["name"];
                $arr=explode('.', $org_filename);
                $filename='resume'.time().'.'.$arr[count($arr)-1];
                $target_dir = UPLOAD_PATH;
                $target_file = $target_dir . basename($filename);
                move_uploaded_file($_FILES["resume"]["tmp_name"], $target_file);

                $_POST['resume']=$filename;
            }

            if(isset($_FILES) && $_FILES["cover_letter"]["name"]!=''){
                $org_filename=$_FILES["cover_letter"]["name"];
                $arr=explode('.', $org_filename);
                $filename='cover_letter'.time().'.'.$arr[count($arr)-1];
                $target_dir = UPLOAD_PATH;
                $target_file = $target_dir . basename($filename);
                move_uploaded_file($_FILES["cover_letter"]["tmp_name"], $target_file);

                $_POST['cover_letter']=$filename;
            }

            if(isset($_FILES) && $_FILES["photo"]["name"]!=''){
                $org_filename=$_FILES["photo"]["name"];
                $arr=explode('.', $org_filename);
                $filename='photo'.time().'.'.$arr[count($arr)-1];
                $target_dir = UPLOAD_PATH;
                $target_file = $target_dir . basename($filename);
                move_uploaded_file($_FILES["photo"]["tmp_name"], $target_file);

                $_POST['photo']=$filename;
            }
$this->general_model->insert('apply_job',$_POST);

Using this code I try to send some data into my database as well as upload files in my server using post method. All data posted fine but files are not uploaded. Showing error while uploading files Undefined index: resume Undefined index: cover_letter Undefined index: photo how to resolve this

Rahul
  • 9
  • 7
  • 1
    there are dozens, if not hundreds, of examples of how to upload a file via ajax available online. Did you check any of them? If you did, you'd see fairly trivially that you need to add some more code and settings on the client side to make it work. – ADyson Aug 24 '17 at 12:46

1 Answers1

-1

Undefined index: resume
Undefined index: cover_letter
Undefined index: photo

It means there are no such records in the $_FILES array, which in turn means that files were not uploaded. It happens because those files contents is not extracted upon form serialization. For solution refer this answer.

Vaviloff
  • 16,282
  • 6
  • 48
  • 56