I am developing an angularJS application using Ionic framework for layout design. For some reason my form is not being submitted. The function is supposed to submit username and password and POST it to an online PHP file. Here is my code:
Template:
<ion-view title="Login">
<ion-content overflow-scroll="true" padding="true" class="has-header">
<?php include '../lib/init.php'; ?>
<form class="list validate" ng-submit="submit()">
<input type="hidden" name="action" value="login">
<ion-list>
<label class="item item-input">
<span class="input-label">Username</span>
<input type="text" ng-model="username" name="username" placeholder="">
</label>
<label class="item item-input">
<span class="input-label">Password</span>
<input type="password" ng-model="password" name="password" placeholder="">
</label>
</ion-list>
<div class="spacer" style="height: 40px;"></div>
<button type="submit" id="login-button4" class="button button-royal button-block">Log in</button>
<a ui-sref="register" id="login-button5" class="button button-royal button-block button-clear">Dont have an account?</a>
<a ui-sref="forgotPassword" id="login-button6" class="button button-royal button-block button-clear">Forgot password?</a>
</form>
</ion-content>
</ion-view>
Controller:
.controller('loginCtrl', function($scope, $state, $stateParams, $ionicPopup, $timeout) {
//popup alert starts here
$scope.showAlert = function(status,message) {
var alertPopup = $ionicPopup.alert({
title: status,
template: message,
});
};
//popup alert ends here
$scope.submit = function() {
if ($scope.username && $scope.password) {
var url = 'my PHP file url';
var username= $scope.username;
var password= $scope.password;
$scope.list = [];
var dataString = 'username='+ username + '&password=' + password;
$scope.list.push(dataString);
$scope.text = '';
$http({
type: "POST",
url: url,
data: dataString,
success: function(data) {
try
{
alert(data);
}
catch(e)
{
alert(data);
}
}
});
}
};
})
Route:
.state('login', {
url: '/login.php',
templateUrl: 'templates/login.php',
controller: 'loginCtrl'
})
Does anyone know whats going on?
usernameandpasswordscope variables, they won't be watched and updated when the user populated the fields. Try adding$scope.username = ''; $scope.password = '';before your$scope.submit = fun....