ISSUE
Greetings! I am encountering an odd behavior with my backend. When I submit data, everything works flawlessly. However, if I press ENTER and submit an empty field, it reposts the previous value. Initially, I cannot submit an empty field, but after entering a value once, I can repeatedly post the same value by hitting ENTER on an empty field. The backend is built on Laravel 5.
MY CODE
HTML
<h1>{{ tasksdata.lists.name }}</h1>
<hr/>
<div ng-repeat="task in tasksdata.tasks">
<h2>{{ task.description }}</h2>
</div>
<h2><a href="javascript:hideshow(document.getElementById('adiv'))"> + </a></h2>
<input type="text" ng-model="taskname" id="adiv" ng-keydown="key($event)" value="" onkeydown="hideshow(document.getElementById('adiv'))" class="form-control" style="font:24px bold; display: none" placeholder="NEW TASK" />
Script
function hideshow(which){
if (which.style.display=="none"){
which.style.display="block";
which.value="";
which.focus();
}
$(which).keydown(function(e) {
if (e.keyCode == 13) {
which.style.display = "none"
}
});
$(which).blur(function () {
which.style.display = "none"
});
}
Angular for POST
myApp.controller('tasksController', ['$scope', '$log', '$http', '$routeParams',
function($scope, $log, $http,$routeParams ){
$http({
method: 'GET',
url: 'http://localhost/anydocopy/public/lists/' + $routeParams.id
})
.success(function (d) {
console.log(d);
$scope.tasksdata = d;
});
var task = {};
console.log('before sending');
console.log(task);
$scope.key = function($event){
console.log($event.keyCode);
if ($event.keyCode == 13) {
task = {
description: $scope.taskname,
list_id: $routeParams.id
};
$http({
method: 'POST',
url: 'http://localhost/anydocopy/public/lists/'+$routeParams.id,
data: task
})
.success(function () {
console.log('true');
task = {};
console.log('after sending');
console.log(task);
$http({
method: 'GET',
url: 'http://localhost/anydocopy/public/lists/' + $routeParams.id
})
.success(function (d) {
console.log(d);
$scope.tasksdata = d;
});
})
.error(function(){
console.log('false');
})
}};
}]);
ATTEMPTS MADE
I have created a new task array before submitting, populated it with HTML data, and then performed the POST operation. If the POST was successful, the array is emptied. Strangely, the console displays an empty array before and after each POST request, yet the same data gets submitted when I hit ENTER. Furthermore, every time I display my input text, its value is always "".