I am uploading the description, the number and the image path
through the PHP
database.
When trying to call the image
from the controller
, an error is displayed which states: Undefined property: stdClass::$imgPath .... on line 9
. On the other hand, the text
elements are uploaded successfully.
Please find the PHP
code below:
<?php
$prov = json_decode(file_get_contents("php://input"));
require_once("connection.php");
$connection = connectToMySQL();
// Check if image file is a actual image or fake image
$proverbDescription = $prov->proverbDescription;
$proverbNumber = $prov->proverbNumber;
$imgPath = $prov->imgPath;
$query = "INSERT INTO tbl_proverb (proverbDescription, proverbNumber, imgPath) VALUES ('$proverbDescription', '$proverbNumber', '$imgPath')";
echo($proverbDescription);
echo($proverbNumber);
echo($imgPath);
if (move_uploaded_file($_FILES['imgPath']['tmp_name'], $target_file)) //for uploading file
{
echo("File uploaded to: " . $target_dir);
}
$result = mysqli_query($connection, $query)
or die("Error in query: ". mysqli_error($connection));
if(mysqli_affected_rows($connection) > 0){
$success = true;
}else{
$success = false;
}
?>
The AngularJS
Controller:
app.controller('proverbCtrl', function($scope, $http, $location) {
$scope.addProverb = function(prov) {
$http.post('php/addProverbs.php', prov).success(function(prov) {
console.log(prov);
// $location.path("/home");
});
};
});
And my HTML
form:
<form id="demo" class="collapse" ng-submit="addProverb(prov)" enctype="multipart/form-data" method="POST">
<label>Image:</label>
<input type="file" ng-model="prov.imgPath" name="imgPath" id="imgPath" accept="image/*">
<label>Proverb Description:</label>
<input type="text" ng-model="prov.proverbDescription" ><br><br>
<label>Proverb Number:</label>
<input type="text" ng-model="prov.proverbNumber"><br><br>
<input type="submit" name="submit"><br>
<a href="#/proverbs">See your post</a>
</form>
If all you need is to get the file name to PHP try something like this:
HTML:
<input type="file" onchange="angular.element(this).scope().fileName(this)"
ng-model="prov.imgPath" name="imgPath" id="imgPath" accept="image/*">
Controller:
$scope.fileName= function(element) {
$scope.$apply(function(scope) {
scope.prov.imgPath = element.name;
});
};