0

I want to Update image using ajax in CodeIgniter. it gives an error that unknown Index "employeePicture". This is the form from which the image is selected for Update.

<form method="post" enctype="multipart/form-data" id="updateData">
<div class="col-md-3">
    <div class="form-group">
        <label for="EditcontactNoSelector">Employee Picture</label>
        <input type="file" name="employeePicture" id="Editemployee_picture">
    </div>
</div>

And This is the Ajax Code.

  var formData = new FormData($("#updateData")[0]);
$.ajax({
    url: "'.base_url().'Employees/master_update_employees",
    type: "post",
    data: formData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    success: function(output) {
        var data = output.split("::");
        if (data[0] === "OK") {
            Shafiq.notification(data[1], data[2]);
            oTable.fnDraw();
            $("#employeePicture").val("");


        } else if (data[0] === "FAIL") {
            Shafiq.notification(data[1], data[2]);
        }
    }
});

And This is the Function where the data updated to the database. now here it gives an error on "employeePicture".

<?php
public

function master_update_employees()
    {
    if ($this->input->post())
        { //If Any Values Posted
        if ($this->input->is_ajax_request())
            { //If Request Generated From Ajax

            // Getting Posted Values

            $employee_picture = $_FILES['employeePicture']['name'];
            $path = 'assets/employee_profile/' . $employee_picture;
            move_uploaded_file($_FILES["employeePicture"]["tmp_name"], $path);
            $Name = $this->input->post('Name');
            $Contact = $this->input->post('Contact');
            $Mobile = $this->input->post('EditMobile');
            $EditCNIC = $this->input->post('EditCNIC');
            $FatherName = $this->input->post('FatherName');
            $ID = $this->input->post('ID');
            $Address = $this->input->post('Address');
            $designation = $this->input->post('EditDesignation');
            $shift = $this->input->post('EditShift');
            $joinDate = $this->input->post('EditJoiningDate');
            $basicSalary = $this->input->post('EditBasicSalary');
            $PermanentAddress = $this->input->post('EditPermanentAddress');
            $IsEnabled = $this->input->post('Enabled');
            $Enabled = 1;
            if ($IsEnabled == "true")
                {
                $Enabled = 1;
                }
            elseif ($IsEnabled == "false")
                {
                $Enabled = 0;
                }

            $table = "employees";
            $updateData = array(
                'Name' => $Name,
                'Father_Name' => $FatherName,
                'Phone' => $Contact,
                'Mobile' => $Mobile,
                'Designation' => $designation,
                'shift' => $shift,
                'JoinDate' => $joinDate,
                'BasicSalary' => $basicSalary,
                'CNIC' => $EditCNIC,
                'Pres_Address' => $Address,
                'Picture' => $path,
                'Perm_Address' => $PermanentAddress,
                'IsEnabled' => $Enabled
            );
            $updateWhere = array(
                'id' => $ID
            );
            $result = $this->Common_model->update($table, $updateWhere, $updateData);
            if ($result === true)
                {
                echo "OK::Record Successfully Updated::success";
                return;
                }
              else
                {
                if ($result['code'] === 0)
                    {
                    echo "FAIL::Record is Same in Database, No Change Occurred::warning";
                    }
                  else
                    {
                    echo "FAIL::";
                    print_r($result);
                    echo "::error";
                    }

                return;
                }
            }
        }
    } // update Employee
7
  • in the first line of the update function add print_r($_FILES) and view the response in dev tools network pane. what is it? just array()? Commented Sep 27, 2018 at 7:15
  • You should check this Answer. Commented Sep 27, 2018 at 7:17
  • it gives me that.Array ( [employeePicture] => Array ( [name] => aneesa.jpg [type] => image/jpeg [tmp_name] => D:\Xampp\tmp\phpC775.tmp [error] => 0 [size] => 18911 ) ) @Alex Commented Sep 27, 2018 at 7:20
  • well then index employeePicture shouldn't be undefined e.g. give you that error. the only way you would get that error is if you don't upload a picture as you don't check to see if it exists beforehand. Commented Sep 27, 2018 at 7:23
  • then why it gives me an error in the network pane. that " Undefined index: employeePicture"?? Commented Sep 27, 2018 at 7:26

1 Answer 1

0

Since you are getting the employee picture fine, I am assuming your issue is when you don't upload a picture as you don't check if it exists. Rearranged your code in this fashion.

$updateData = array(
                    'Name' => $Name,
                    'Father_Name' => $FatherName,
                    'Phone' => $Contact,
                    'Mobile' => $Mobile,
                    'Designation'=>$designation,
                    'shift'=>$shift,
                    'JoinDate'=>$joinDate,
                    'BasicSalary'=>$basicSalary,
                    'CNIC' => $EditCNIC,
                    'Pres_Address' => $Address,
                    //'Picture' => $path,
                    'Perm_Address' => $PermanentAddress,
                    'IsEnabled' => $Enabled
                );

if (isset($_FILES['employeePicture'])) {
    $employee_picture=$_FILES['employeePicture']['name'];
    $path='assets/employee_profile/'.$employee_picture;
    move_uploaded_file($_FILES["employeePicture"]["tmp_name"], $path);
    $updateData['Picture'] = $path;
}
Sign up to request clarification or add additional context in comments.

2 Comments

This Code Not Working
not working isn't constructive. what exactly isn't working?

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.