AgnularJS version is v1.5.6
When an user input data and submit the form data will be store in mySQL using PHP. but I got error < JSON.parse: unexpected character > in firefox.
in my DB_formSave.php, I have whole code such as creating table.. insert query.. but first, I want to make sure it's connection db and angularJS
Plus, anyone can tell me php can only aceept JSON format or Object? and AngulrJS can only accpet JSON or Object? I am pretty new so please share your knowledge to me.
controller.js
var userControllers = angular.module('userControllers', []);
userControllers.controller('UserFormController', ['$scope', '$http',
'$location', '$window', 'myFactory', function ($scope, $http, $location,
$window, myFactory) {
var dataObj = "";
var config = { headers: { 'Content-Type': 'application/x-www-form urlencoded;charset=utf-8;' } }
$scope.submitForm = function (user) {
myFactory.set(user); // Set data to myFactory to share with ConfirmationController
dataObj = {
"name": user.name,
"province": user.province,
"telephone": user.telephone,
"postalcode": user.postalcode,
"salary": user.salary
}
console.log(dataObj); //it shows this format like below :
Object { name: "Jason", province: "Québec", telephone: "(416) 123-1234", postalcode: "M2N3L3", salary: "40.000" }
$http.post('ajax/DB_formSave.php', dataObj)
.success(function(data) {
console.log(data);
console.log(dataObj);
})
}
}]);
DB_formSvae.php
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$post_date = file_get_contents("php://input");
$data = json_decode($post_date);
$servername = "localhost:3307";
$username = "root";
$password = "";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// check connection
if($conn->connect_error){
die("Connection failed:".$conn->connect_error);
}
echo "Connected successfully
";
?>