Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
This is too broad. Just Google PHP and database and you'll get plenty of tutorial. –  John Conde Oct 5 '14 at 22:16
    
please help if you can , i've google it already and can't made it –  user3856699 Oct 5 '14 at 22:18
2  
If you can't follow any of the tutorials posted on the Internet already, how will you follow one posted here? –  John Conde Oct 5 '14 at 22:19
    
because they are not simple as i want , as i said before i'm new in angular –  user3856699 Oct 5 '14 at 22:22
    
Why do you think it would be any simpler here? Also, if you read my first comment again, you'll notice I said PHP and database. I didn't mention Angular. Angular cannot interact with a database. –  John Conde Oct 5 '14 at 22:52

3 Answers 3

<!-- try to do with this example -->   
 <html ng-app>
        <head>
            <title>AngularJs Post Example: DevZone.co.in </title>
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
            <style>
                #dv1{
                    border:1px solid #DBDCE9; margin-left:auto;
                    margin-right:auto;width:220px;
                    border-radius:7px;padding: 25px;
                }

                .info{
                    border: 1px solid;margin: 10px 0px;
                    padding:10px;color: #00529B;
                    background-color: #BDE5F8;list-style: none;
                }
                .err{
                    border: 1px solid;  margin: 10px 0px;
                    padding:10px;  color: #D8000C;
                    background-color: #FFBABA;   list-style: none;
                }
            </style>
        </head>
        <body>
            <div id='dv1'>
                <form ng-controller="FrmController">
                    <ul>
                        <li class="err" ng-repeat="error in errors"> {{ error}} </li>
                    </ul>
                    <ul>
                        <li class="info" ng-repeat="msg in msgs"> {{ msg}} </li>
                    </ul>
                    <h2>Sigup Form</h2>
                    <div>
                        <label>Name</label> 
                        <input type="text" ng-model="username" placeholder="User Name" style='margin-left: 22px;'> 
                    </div>
                    <div>
                        <label>Email</label>
                        <input type="text" ng-model="useremail" placeholder="Email" style='margin-left: 22px;'> 
                    </div>
                    <div>
                        <label>Password</label>
                        <input type="password" ng-model="userpassword" placeholder="Password">
                    </div>
                    <button ng-click='SignUp();' style='margin-left: 63px;margin-top:10px'>SignUp</button>
                </form>
            </div>

            <script type="text/javascript">
                function FrmController($scope, $http) {
                    $scope.errors = [];
                    $scope.msgs = [];

                    $scope.SignUp = function() {

                        $scope.errors.splice(0, $scope.errors.length); // remove all error messages
                        $scope.msgs.splice(0, $scope.msgs.length);

                        $http.post('post_es.php', {'uname': $scope.username, 'pswd': $scope.userpassword, 'email': $scope.useremail}
                        ).success(function(data, status, headers, config) {
                            if (data.msg != '')
                            {
                                $scope.msgs.push(data.msg);
                            }
                            else
                            {
                                $scope.errors.push(data.error);
                            }
                        }).error(function(data, status) { // called asynchronously if an error occurs
    // or server returns response with an error status.
                            $scope.errors.push(status);
                        });
                    }
                }
            </script>
            <a href='http://devzone.co.in'>Devzone.co.in</a>
        </body>
     </html>
<?php

$data = json_decode(file_get_contents("php://input"));
$usrname = mysql_real_escape_string($data->uname);
$upswd = mysql_real_escape_string($data->pswd);
$uemail = mysql_real_escape_string($data->email);

$con = mysql_connect('localhost', 'root', '');
mysql_select_db('test', $con);

$qry_em = 'select count(*) as cnt from users where email ="' . $uemail . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);

if ($res['cnt'] == 0) {
    $qry = 'INSERT INTO users (name,pass,email) values ("' . $usrname . '","' . $upswd . '","' . $uemail . '")';
    $qry_res = mysql_query($qry);
    if ($qry_res) {
        $arr = array('msg' => "User Created Successfully!!!", 'error' => '');
        $jsn = json_encode($arr);
        print_r($jsn);
    } else {
        $arr = array('msg' => "", 'error' => 'Error In inserting record');
        $jsn = json_encode($arr);
        print_r($jsn);
    }
} else {
    $arr = array('msg' => "", 'error' => 'User Already exists with same email');
    $jsn = json_encode($arr);
    print_r($jsn);
}
?>
share|improve this answer
    
Good job Pankaj, It works fine! –  Dreamer Dec 23 '14 at 9:24

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

share|improve this answer

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);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.