1

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

1 Answer 1

4

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);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I need to read up on ng-model a bit more this will really help me.
Leaving action="" like this might break the ng-submit in some web browsers -- Even tho it's empty, it might still submit the form via GET.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.