looking for some solutions? You are welcome.

SOLVED: How to get values from input boxes and save to database with button click angular

Mitko:

I'm still learning to programme on AngularJS. I want to get values from the input box and then click on the button I want to save them in the database. My database table is called "user".I have columns like id,username,passsword,name. I have two checkbox.When i click on first checkbox(Edit) it displaying "div myVar",when i click on second checkbox(Add new user) it displaying "div myVar1". I want to activate this button "Add" to add input box values to database mysql. Please help me. I have this html code:

<label><input type="checkbox" ng-model="myVar">Edit</label>
<label><input type="checkbox" ng-model="myVar1">Add new user</label>
    <div ng-show="myVar">
    <div class="form">
    <form ng-submit="saveItem(userForm.$valid)" name="userForm">
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label for="database_address">User</label>
                    <input type="text" class="form-control" required ng-model="activeItem.username" placeholder="Потребителско Име..." />
                </div>

                <div class="form-group">
                    <label for="password">Password</label>
                    <input type="text" class="form-control" required id="password" ng-model="activeItem.password"  />
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label for="username">Operator</label>
                    <input type="text" class="form-control" required id="username" ng-model="activeItem.name" />
                </div>
            </div>
        </div>
        <button class="btn btn-primary" ng-disabled="userForm.$invalid" type="submit">Save</button>
        <!--<button class="btn btn-primary" ng-disabled="userForm.$invalid" type="submit">Добавяне на нов</button>-->
    </form>
    </div>

    <!----------------------------FORM FOR ADD USER---------------------------->
    <div ng-show="myVar1">
    <div class="form1">
    <form ng-submit="createUser(userForm.$valid)" name="userForm1">
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label for="database_address">User</label>
                    <input type="text" class="form-control1"  placeholder="Потребителско Име..." />
                </div>

                <div class="form-group">
                    <label for="password">Password</label>
                    <input type="text" class="form-control1" required id="password"   />
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label for="username">Operator</label>
                    <input type="text" class="form-control1" required id="username"  />
                </div>
            </div>
        </div>
        <button class="btn btn-primary" ng-disabled="userForm.$invalid" type="submit">Add</button>
        <!--<button class="btn btn-primary" ng-disabled="userForm.$invalid" type="submit">Добавяне на нов</button>-->
    </form>
    </div>
    </div>
    <!----------------------------FORM FOR ADD USER---------------------------->

    </div>

And Angular code:

$scope.createUser=function()
{
    console.log($scope.activeItem);
        //delete $scope.activeItem.hash_method
        var objectToSave = {
            username: $scope.activeItem.username,
            password: $scope.activeItem.password,
            name: $scope.activeItem.name,
            id: $scope.activeItem.id
        };
         {
            defaultAdapter.query('INSERT INTO users(username,password,name,id) VALUES(username = :username, password = :password,name = :name WHERE id =:id)',
              { replacements: objectToSave, type: Sequelize.QueryTypes.UPDATE }
            ).then(projects => {
              console.log(projects);
              $scope.editMode = false;
                $scope.activeItem = false;
                $scope.refresh();
            });
        }
}



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent