Hi Im having an issue with uploading files (image/avatar) using angularJS I've tried multiple ways but I'm not able to figure it out
All the other input fields are sending the data correctly from index.php to insert.php which handles the MySQL data processing
in the below script Im just attempting to read the value of ng-model
avatar, but Im not getting it
this is the angularJS
var app = angular.module('crudApp', ['datatables', 'ngSanitize']);
app.controller('crudController', function($scope, $http) {
$scope.submitForm = function(){
console.log($scope.avatar);
$http({
method:"POST",
url:"supervisor/insert.php",
data:{
'avatar':$scope.avatar,
'sup_name':$scope.sup_name,
'sup_email':$scope.sup_email,
'ip_addr':$scope.hidden_id,
'action':$scope.submit_button,
'id':$scope.hidden_id}
}).success(function(data){
if(data.error != '')
{
$scope.success = false;
$scope.error = true;
$scope.errorMessage = data.error;
}
else
{
$scope.success = true;
$scope.error = false;
$scope.successMessage = data.message;
$scope.form_data = {};
$scope.closeModal();
$scope.fetchData();
}
});
};
when I add console.log($scope.avatar);
it shows that the value avatar is null
HTML <input type="file" ng-model="avatar" name="avatar">
insert.php
$form_data = json_decode(file_get_contents("php://input"));
$error = array();
$message = '';
$validation_error = '';
$avatar = '';
$sup_name = '';
$sup_email = '';
$ip_addr = '';
//handle upload avatart
if($form_data->action == 'fetch_single_data')
{
$query = "SELECT * FROM $sup_table WHERE id='".$form_data->id."'";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
$output['avatar'] = $row['avatar'];
$output['sup_name'] = $row['sup_name'];
$output['sup_email'] = $row['sup_email'];
$output['ip_addr'] = $row['ip_addr'];
}
}
elseif($form_data->action == "Delete")
{
$query = "
UPDATE $sup_table set status = '1' WHERE id='".$form_data->id."'
";
//DELETE FROM $sup_table WHERE id='".$form_data->id."'
$statement = $connect->prepare($query);
if($statement->execute())
{
$output['message'] = 'Supervisor data has been removed';
}
}
else
{
if(empty($form_data->avatar))
{
$error[] = 'avatar is empty';
}
else
{
$avatar = $form_data->avatar;
}
appreciate any guidance
related to File Upload using AngularJS