1

I have tried to look for tutorials on this but nothing simple and to the point...

Could anyone give me an example or link me to a tutorial on how to post data with angularjs to a php page?

And also with angularjs call a php file to get data in order to display a list of lets say a phonebook.

How can this be done with angularjs? I can do all this with php but i want to know how to do it with Angularjs to visualize and call php files and those PHP files would read/write on the db.

I want to use plain php(no php frameworks for now).

Thanks

Miguel_Velazkez
  • 180
  • 2
  • 12

3 Answers3

1

You can do it making in PHP a couple of RESTfull services for the CRUD operation, for this you can use a Framework like Slim.

Then in AngularJs you can make a service like this for access the data:

app.factory('phoneService', ['$resource',
    function ($resource) {
        return $resource('../app/phone/:id',{id : '@id'},
            { 'findOne': { method: 'GET', params : {id : '@id'}}, isArray:false },
            { 'delete' : { method: 'DELETE', params : {id : '@id'}}, isArray: false },
            { 'save' : {method: 'POST', params : {id : '@id'}}, isArray: false }
        );
    }
]);
Gonz
  • 1,198
  • 12
  • 26
  • Thanks man, but I actually want to use plain php. I want to use angular to call/post to the php files, and do operations without having to leave the page. Is there a way to do that? – Miguel_Velazkez Mar 05 '14 at 13:03
  • Fist at all, you don't need AngularJs for doing that. You could use another JavaScript Framework. For ex. check this example: http://coenraets.org/blog/2011/12/restful-services-with-jquery-php-and-the-slim-framework/ About what you ask, you could still use what I post, just make the RESTFull services in plain PHP. – Gonz Mar 05 '14 at 13:33
  • I ended up using Slim framework and JQuery... I didn't have knowledge of JQuery and i got it faster than Angular... and it works very well. Thanks! I will mark your reply as the answer. One question... I'm using get requests in a public view. I don't want to make Post requests since you can easily see the source code. There is no way to hid the JQuery source code right? Since you can make calls to the Restful service if you see how they are made the source code. – Miguel_Velazkez Mar 16 '14 at 01:33
  • Glad you could do it. Remember that you should use the GET for everything. Look at this: http://stackoverflow.com/questions/4573305/rest-api-why-use-put-delete-post-get And for your concern about the security, you should add a token to the request, so you can verify that it is a trusted request. You could also pass all your Js by a code minifier like this: http://jscompress.com/ Hope it's help – Gonz Mar 16 '14 at 16:59
  • 1
    I'm sorry i got lost as you say to use GET for everything. The token could bee seen from the source code? Like when looking at the source someone could make requests. Get request wouldn't be the problem but what about post,put or anything that could modify the database. The workaround is that i would then put a php admin login page for doing so. Maybe that was pretty obvious and i was over thinking hehe – Miguel_Velazkez Mar 17 '14 at 01:36
0

May be it will help you.It is a simple search form submission.

http://www.keyboardninja.eu/webdevelopment/a-simple-search-with-angularjs-and-php

http://devzone.co.in/angularjs-simple-example-post-data-php-page/

Anna
  • 36
  • 7
0

After a long search, I've found a simple and best guide for AngularJS beginners. This example AngularJS application contain the following functionalities.

  • Fetch data from the database using PHP & MySQL, and display data using AngularJS.
  • Add data to the database using AngularJS, PHP, and MySQL.
  • Edit and update data using AngularJS, PHP, and MySQL. Delete user from the database using AngularJS, PHP, and MySQL.

If you are looking for a simple AngularJS application with step-by-step guide, must check this tutorial - AngularJS CRUD Operations with PHP and MySQL

JoyGuru
  • 1,803
  • 20
  • 11