Ausghostdog
Ausghostdog

Reputation: 178

How to use put method to update a database from a form

I'm trying to update a database from a form without using any php, I'm using angularjs with MySql

If I use the following with the put method I can insert a user into the data base, I've

http://localhost:8080/CyberSolution/rest/user/register?u=tester&pw=fred&fname=Fred&lname=Smith&[email protected]

I've made a basic html form

<form method="POST" action="">

  Username: <input type="text" name="username" size="15" /><br />

  Password: <input type="password" name="password" size="15" /><br />
  email: <input type="text" name="email" size="15" /><br />
  First name: <input type="text" name="first_name" size="15" /><br />
  Last name: <input type="text" name="lirst_name" size="15" /><br />
    <p><input type='submit' name='Submit' value='Submit' /></p>

</form>

I'm working with AJS for the first time and also REST for the first time, I'm unsure on how to link the form to the database to update. I'm unsure what needs to go in the controllers.js as well

Thanks

Upvotes: 1

Views: 1998

Answers (1)

Riron
Riron

Reputation: 1023

You could simply use the $http service.

First use ng-model to store fields value in an object :

<form ng-submit="send_form()" action="">

  Username: <input type="text" name="username" size="15" ng-model="form.username" /><br />

  Password: <input type="password" name="password" size="15" ng-model="form.password"/><br />
  email: <input type="text" name="email" size="15" ng-model="form.email"/><br />
  First name: <input type="text" name="first_name" size="15" ng-model="form.first_name"/><br />
  Last name: <input type="text" name="lirst_name" size="15" ng-model="form.last_name"/><br />
    <p><input type='submit' name='Submit' value='Submit' /></p>

</form>

And then use in your controller (or even better, through a service) :

function myController($scope, $http) {
    $scope.send_form = function () {
       $http.put('http://localhost:8080/CyberSolution/rest/user/register', $scope.form).success(successCallback);
    }
}

Upvotes: 4

Related Questions