Reputation: 13
I am trying to insert img name into database.
<img class="modal-trigger" href="#modal1" src="img\test.png" onclick="getName()" id="img1" />
I am getting name of image using Javascript and also inserting into database in angularJS.
var dept;
function getName() {
var fullPath = document.getElementById("img1").src;
dept = fullPath.replace(/^.*[\\\/]/, '');
document.getElementById("result").value = dept;
}
$scope.addnew = function() {
$http.post('addtodb.php',{'username' : $scope.username, 'dept' : $scope.dept}
).success(function (data, status, headers, config) {
});
}
Getting name of image works fine when I 'echo' it and also adding into database works cause I can add username. But how can I send dept variable into function addnew and put it into database?
Upvotes: 1
Views: 83
Reputation: 69
You can replace dept = fullPath.replace(/^.*[\\\/]/, '')
with
$scope.dept = fullPath.replace(/^.*[\\\/]/, '')
.
Or Since you need the getName()
function to run before $scope.addNew()
, you could call the $scope.addNew()
function after the getName()
and pass in the dept
as an argument.
For instance:
Inside the function getName()
, you call $scope.addNew(dept)
and access the argument using $scope.addnew = function(dept)
.
Upvotes: 1