2017-02-13 71 views
1

我试图将img名称插入到数据库中。将图像名称插入到数据库中

<img class="modal-trigger" href="#modal1" src="img\test.png" onclick="getName()" id="img1" /> 

我使用Javascript获取图像的名称,并在angularJS中插入数据库。当我用“echo”它,并且还加入到数据库中的作品,因为我可以添加用户名

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) { 
     }); 
    } 

获取图像的名称正常工作。但我怎样才能发送dept变量到函数addnew并把它放入数据库?

+0

这是一个角度问题,而不是PHP,PLZ不标记irelevant的东西 – Auris

回答

1

您可以用 $scope.dept = fullPath.replace(/^.*[\\\/]/, '')替换dept = fullPath.replace(/^.*[\\\/]/, '')。 或者由于您需要getName()函数在$scope.addNew()之前运行,因此可以在getName()之后调用$scope.addNew()函数,并将dept作为参数传入。 例如: 在函数getName()中,您可以调用$scope.addNew(dept)并使用$scope.addnew = function(dept)访问参数。

+0

那样?不起作用 http://pastebin.com/sYA5TAvH – Radekesd

+0

当您将变量'dept'作为参数(比如'$ scope.addNew(dept)')传递时,您将需要修改post请求以使用传递的参数如:'$ http.post('addtodb.php',{'username':$ scope.username,'dept':dept} )' – Ethan

+0

http://pastebin.com/dUA05xjs仍然没有我得到空部门记录 – Radekesd

相关问题