'Saving Data from form to database using AngularJS and php
I'm pretty new in angular.
i created a modal window to user share story and after that show it in body:
html
<button class="btn btn-primary new-story" ng-click="showPopup()">New Story</button>
<div class="wroteStory img-rounded" ng-repeat="story in sentCompose">
<h1 class="s-label"><i><label for="subject">Subject :</label></i></h1>
<h5>{{story.subject}}</h5>
<h1 class="s-label"><i><label for="body">Full Story :</label></i></h1>
<h6>{{story.body}}</h6>
<button class="btn btn-danger" ng-click="remove($index)"> Delete </button>
<hr/>
</div>
js
app.controller('aboutController', function($scope,ngProgress) {
$scope.popupVisible = false;
$scope.showPopup = function(){
$scope.popupVisible = true;
$scope.composeStory = {};
}
$scope.closePopup = function(){
$scope.popupVisible = false;
}
$scope.composeStory = {};
$scope.sentCompose = [];
$scope.sendStory = function() {
$scope.sentCompose.push($scope.composeStory);
$scope.popupVisible = false;
$http.post("insert.php", data).success(function(data, status, headers, config){
});
};
i want to save data from this form to database ? Thx in advance
Solution 1:[1]
first you should be able to create a db in mysql and creat a table for that .
then you should be able to connect them with php like this :
$host = "localhost";
$user = "angular";
$pass = "angular";
$database = "angular";
$con = mysql_connect($host,$user,$pass);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db($database,$con);
Solution 2:[2]
With limited information you provided, i will try to help you on this. When you ask questions, please show what output your are getting, what your debugging says. It will be helpful for others to understand your problem and suggest you some solution. Here is my suggestions
1) I could not see scope of your $http.post("insert.php", **data**)
data variable make sure that you have serialized data in it.
2) Check in firebug whether your request is being sent or not. If you can see the request then see what is the response you are getting
3) As you have success handler always have a error handler for any Ajax call, it is a best practice and saves lot of your time in debugging
My suggestions are based on assumption that your insert.php
is tested for inserting data properly. If not you have to follow what John Conde told
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sadeghbayan |
Solution 2 | Hassaan |