• How to Implement Add/Edit/Delete/View with PHP using Angular JS (Part-1)


    http://tech-blog.maddyzone.com/javascript/perform-addeditdeleteview-php-using-angular-js


    How to use CRUD(create read update delete)  in angular With PHP ?

    Our Blog Regarding Interaction Angular JS with PHP teaches you :

    How to –

    – Add Product
    – Edit Product
    – Delete Product
    – View Product in Grid Form using below Example using Angular JS and PHP.

    To Interact Angular JS with PHP We have to follow following Steps:

    First Step :

    – First , we have to create a database wih name “Shopping” while open phpmyadmin after starting “Xampp or Wamp Server” for PHP.

    – We will follow the below Database Structure as mention in snapshot.

    Database Structure

    add edit delete db in angular

    Snapshot showing a table name as “product”, having fields like :

    •  id (Unique and Auto increment ID field for a product)
    •  prod_name (Field having value of Name of Product)
    •  prod_price (Field having value of Price of Product)
    •  prod_quantity (Field having value of Total Quantity of Product)

    after creating this table in phpmyadmin we completed database for our example.
    Now the next step is how we will use this database structure in our most required example.

    Second Step :

    – For this, we simply create a below folder structure in htdocs folder of Xampp Server.

    – Folder name showing angular example like : angular_example

    – After creating a folder we create our first php file for this by name “config.php” in this folder. config.php file have below detail for database connectivity with database we created in First Step:

    Config.php file

    – Now we create a new html file for our example which have html using form tag to show “Form GUI” on web page to interact with user for adding product and other action.

    – So for this our third step starts here:

    Third Step :

    – We crate a html file by name “angular_example.html”.
    – angular_example.html file have html for form to Add Product and other action , as we have to add product using angular js and php so we have to include angular js firstly in our html.
    – To include angular js we follow the below statement :

    – To Performing Angular Action we will use controller.js, we will discuss more about controller.js, later in next step.

    – By using the below code , we created an html structure of form to add product.

    – Now the question arises , how angular js will work here, Dear if you know basis of angular js than it so easier to use angular js with html to perform submit form and get field values using property “ng-controller” to specify controller for html page we have created where angular will call for first stage. If you are stuck here then please follow this link to learn angular js basis fundamentals:

    http://tech-blog.maddyzone.com/javascript/learn-complete-angularjs-in-5-steps-part-1

    If you are familier with angular js then we now move to our next step:

    Fourth Step :

    – Here we create a new file named as “controller.js” in the same folder.

    – controller.js file have name of controller “PhoneListCtrl” using “listapp” module, which controlles the viewing of data on html and get data from html for form elements.

    – Controller “PhoneListCtrl” will have scope function to add/edit/delete/product data to database table we created in first step and to show data on html in grid form.

    Now the question will arise in your mind how client side controller will interact to server side database?

    My dear , to do this, we pass an attribute $http with scope variable in contoller function as below :


    – $http is most important or key of this precious example, used to get and post data from database and to send data to database respectively using php (Server side scripting language) and an interface has been created between client side and server side for interaction.

    Now Our Next Step will come which shows you how $http is used for interaction, so now next step is ….
    Fifth Step:

    – In this step , we create a php file name as “db.php” in same folder.

    – “db.php” file at first interact with database connection establishment, so for this we include our config.php file , which we created in step-2 by below code :

    – After Establisment an connection with database, “db.php” will get action from our angular js controller named “controller.js” to check if a form action is performed or not?

    – Now you are thinking how i send an action and what action i have to send from html to controller.js ?

    – So now we will send form submit action to controller by below code which will later retrive on db.php

    – After seeing this code you got stuck ohh!

    – How it is, so don’t stuck here , its so easy for you:

    – We just create a product_submit action from html by below code , and this action gets the values of form element using $scope variable.

    – As you see $http.post method is used to send form data to db.php with action name as “add_product” itself representing to add product.

    – Now you can check how html for form is for this so you can check below the code :

    – $scope.prod_name showing the ng-model attribute of angular js to get field value by calling “product_submit” function mention by code above in html for button.

    = By using this function we will get the values of form fields in db.php by reteriving the action name from the url and using below code:

    – As we get an action add_product using switch case for other action as well like edit/delete/view product , to add a product we call a function in db.php add_product.

    – add_product function get fields value which are json encoded from angular js file name “controller.js” by function product_submit.

    – so , our first step to get fields values in a variable to save into database, for this we have to decode the json first.

    – To decode the json we will use the below code:

    – Now all form fields value are in $data object by key name as we specify in product_submit function in controller.js if you remind?

    – To remeber you we are pasting the snippet below :

    word written in ”(single quote) representing the keys.

    So, now it easier for you here to get the values in php variables by using below code :

    yeah !! here you get success to getting data from html to php using angular js.

    – To add this fields values in our product table of shopping database so easier for you using the below code

    So complete add product function now is as :

    Product Add

    In the same way you can work for update / delete form data by creating function for update and delete in db.php and controller.js.
    Now Next Step is how to show data to html which we save in database using db.php using angular js to complete our motto of this example.

    so for this we are on final step of this precious example as with next step:

    Sixth Step:

    – To view or show data on html what we have save using product_submit method of controller.js file and add_product method of db.php file in fifth step we create a function by name get_product in db.php using below code :

    – get_product() function get data from db using php in array form but we need this data in json form to show on html using angular js, so first we convert array of data of all records or product we added using add_product and after performing other action like edit/delete.

    – As we get json data we return json data by return statement of this function as code below :

    – Now you can get the complete db.php file here.

    db.php file

    – Now we get all data from database and we have to show this data on our html in grid form using angular js, so to do this, we create a function using below code in controller.js file.

    –  get_product function in controller.js call the get_product function of db.php file using get method of $http variable passed with $scope in controller function if you remind, if not we are here to remind you, check the below snippet.

    – If we get success in getting data from database using db.php , here we use below code to present data on html mentioned in above function if you can see.

    So as we got all success in add/edit/delete/view using function’s in controller.js , you can get the complete code for controller.js here.

    controller.js file

    – As we know from above the data comes in json form using get_product function what we use in db.php and return, so to represent this multiple row data on html we follow the loop to present every row data with their respective columns and present in table format as in below code:

    – ng-init property call get_product function of controller.js and get all json data, now for each row data

    reterival use ng-repeat property in same way as foreach loop of php. and by using the product object get
    the fields value by their key name and show as table column.

    – Here we also create link for edit and delete of a product, by which on click of these link respective function first called controller.js and their respective function call edit/delete function specify in db.php.

    – so, as you complete this tutorial very much you get here the complete html of this precious example.

    HTML file

  • 相关阅读:
    性能计数器
    SpringBoot_数据访问-整合JPA
    SpringBoot_数据访问-整合MyBatis(二)-注解版MyBatis
    SpringBoot_数据访问-整合Druid&配置数据源监控
    SpringBoot_数据访问-JDBC&自动配置原理
    八字克妻口诀
    没想到,我能这么细心地看算法
    辰戌丑未,四库
    The jar of divisors
    分布式系统阅读笔记(十六)-----事务和并发控制
  • 原文地址:https://www.cnblogs.com/ztguang/p/12649636.html
Copyright © 2020-2023  润新知