Im using xampp server in windows
In Ionic v1
create the api directory in www folder of your xampp, then create a file called signup.php
in your controller, put this code
.controller('registerCtrl', function ($scope, $http, $ionicPopup, $state, $ionicHistory, $ionicLoading) {
$scope.$on('$ionicView.enter', function () {
//Clear the Registration Form.
$scope.user = {
studentid: '',
password: '',
email: '',
password`Preformatted text`: '',
firstname: '',
lastname: ''
};
});
$scope.user = {};
$scope.signup = function () {
$ionicLoading.show({
template: '<ion-spinner icon="android"></ion-spinner>'
});
var link = 'http://localhost/api-dir/signup.php';
$scope.studentid = document.getElementById('studentid').value;
$scope.firstname = document.getElementById('firstname').value;
$scope.lastname = document.getElementById('lastname').value;
$scope.email = document.getElementById('email').value;
$scope.password = document.getElementById('password').value;
$http.post(link, { studentid: $scope.studentid, firstname: $scope.firstname, lastname: $scope.lastname, email: $scope.email, password: $scope.password})
.then(function (res) {
$ionicLoading.hide();
$scope.response = res.data.result;
// alert(JSON.stringify($scope.response));
//alert($scope.response);
if ($scope.response.created == "1") {
$scope.title = "Successful!";
$scope.template = "You have successfully created your account";
$state.go('login', {}, { location: "replace", reload: true });
} else if ($scope.response.exists == "1") {
$scope.title = "Already Exist";
$scope.template = "Student with this information already exist";
} else {
$scope.title = "Failed";
$scope.template = "Contact Our Technical Team";
}
var alertPopup = $ionicPopup.alert({
title: $scope.title,
template: $scope.template
});
});
};
$ionicHistory.nextViewOptions({
disableAnimate: true,
disableBack: true
});
})
and your signup.php can look like this
<?php
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
$postdata = file_get_contents("php://input");
if (isset($postdata)) {
$request = json_decode($postdata);
$studentid=$request->studentid;
$email=$request->email;
$password=$request->password;
$firstname=$request->firstname;
$lastname=$request->lastname;
//$password=md5($password);
$conn = new mysqli("localhost", "dbusername", "dbpassword", "dbname");
$studentid = stripslashes($studentid);
$email = stripslashes($email);
$password = stripslashes($password);
$firstname = stripslashes($firstname);
$lastname = stripslashes($lastname);
$studentid = $conn->real_escape_string($studentid);
$email = $conn->real_escape_string($email);
$password = $conn->real_escape_string($password);
$firstname = $conn->real_escape_string($firstname);
$lastname = $conn->real_escape_string($lastname);
$datereg = date('d/M/Y');
$check="SELECT count(*) FROM register WHERE student_id = '$studentid'";
$rs = mysqli_query($conn,$check);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);
if($data[0] > 0) {
$outp='{"result":{"created": "0" , "exists": "1" } }';
}
else{
$sql = "INSERT INTO register (`student_id`,`firstname`,`lastname`,`email`,`password`,`reg_date`) VALUES ('$studentid','$firstname','$lastname','$email','$password','$datereg')";
if ($conn->query($sql) === TRUE) {
$outp='{"result":{"created": "1" , "exists": "0" } }';
}
else{
$outp='{"result":{"created": "-1" , "exists": "0" } }';
}
}
echo $outp;
$conn->close();
}
?>
and create your view appropriately
Since you asked the question in Ionic V1 lodge, that’s a not very clean way to do it but it works very well