• We just launched and are currently in beta. Join us as we build and grow the community.

PHP - Simple Insert MySQLi Data Using AngularJS

N3cro

Retro Anime Fan
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
159
Likes
31
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial we will create a Simple Insert MySQLi Data Using AngularJS. This code will submit the MySQLi POST data when the user click the submit button. The code use angular directives to insert the form data that enable to send a POST request to the MySQLi server. This a user-friendly program feel free to modify and use it to your system.

We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Lastly, this is the link for the AngularJS https://angularjs.org/.

Creating Database

Open your database web server then create a database name in it db_angular_insert, after that click Import then locate the database file inside the folder of the application then click ok.
2019-07-18_23_22_34-localhost_127.0.0.1_db_angular_insert_phpmyadmin_4.8.3.png

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "db_angular_insert"

    )

    ;

  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  6. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it index.php.
  1. <!

    DOCTYPE html>
  2. <

    html lang=

    "en"

    ng-

    app=

    "myModule"

    >
  3. <

    head>
  4. <

    meta charset=

    "UTF-8"

    name=

    "viewport"

    content=

    "width=device-width, initial-scale=1"

    />
  5. <

    link

    rel=

    "stylesheet"

    type=

    "text/css"

    href=

    "css/bootstrap.css"

    />
  6. </

    head>
  7. <

    body ng-

    controller=

    "myController"

    >
  8. <

    nav class

    =

    "navbar navbar-default"

    >
  9. <

    div class

    =

    "container-fluid"

    >
  10. <

    a class

    =

    "navbar-brand"

    href=

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a>
  11. </

    div>
  12. </

    nav>
  13. <

    div class

    =

    "col-md-3"

    ></

    div>
  14. <

    div class

    =

    "col-md-6 well"

    >
  15. <

    h3 class

    =

    "text-primary"

    >

    PHP -

    Simple Insert MySQLi Data Using AngularJS</

    h3>
  16. <

    hr style=

    "border-top:1px dotted #ccc;"

    >
  17. <

    div class

    =

    "col-md-4"

    >
  18. <

    form>
  19. <

    div class

    =

    "form-inline"

    >
  20. <

    input type=

    "text"

    class

    =

    "form-control"

    ng-

    model=

    "firstname"

    placeholder=

    "Firstname"

    />
  21. <

    input type=

    "text"

    class

    =

    "form-control"

    ng-

    model=

    "lastname"

    placeholder=

    "Lastname"

    />
  22. <

    input type=

    "text"

    class

    =

    "form-control"

    ng-

    model=

    "address"

    placeholder=

    "Address"

    />
  23. <

    br /><

    br />
  24. <

    center><

    button type =

    "button"

    class

    =

    "btn btn-primary"

    ng-

    click =

    "insetDats()"

    ><

    span class

    =

    "glyphicon glyphicon-save"

    ></

    span>

    Insert</

    button></

    center>
  25. </

    div>
  26. </

    form>
  27. </

    div>
  28. <

    div class

    =

    "col-md-8"

    >
  29. <

    table class

    =

    "table table-bordered"

    >
  30. <

    thead class

    =

    "alert-info"

    >
  31. <

    tr>
  32. <

    th>

    Firstname</

    th>
  33. <

    th>

    Lastname</

    th>
  34. <

    th>

    Address</

    th>
  35. </

    tr>
  36. </

    thead>
  37. <

    tbody>
  38. <

    tr ng-

    repeat =

    "member in members"

    >
  39. <

    td>

    {

    {

    member.

    firstname}

    }

    </

    td>
  40. <

    td>

    {

    {

    member.

    lastname}

    }

    </

    td>
  41. <

    td>

    {

    {

    member.

    address}

    }

    </

    td>
  42. </

    tr>
  43. </

    tbody>
  44. </

    table>
  45. </

    div>
  46. </

    div>
  47. <

    script src =

    "js/angular.js"

    >

    </script>
  48. <

    script src =

    "js/script.js"

    >

    </script>
  49. </

    body>
  50. </

    html>

Creating the Main Function

This code contains the main function of the application. This code will insert a POST data when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as shown below.
insert.php
  1. <?php
  2. require_once

    'conn.php'

    ;
  3. $data

    =

    json_decode

    (

    file_get_contents

    (

    "php://input"

    )

    )

    ;

  4. $firstname

    =

    $data

    ->

    firstname

    ;
  5. $lastname

    =

    $data

    ->

    lastname

    ;
  6. $address

    =

    $data

    ->

    address

    ;


  7. mysqli_query

    (

    $conn

    ,

    "INSERT INTO `member` VALUES('', '$firstname

    ', '$lastname

    ', '$address

    ')"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  8. ?>

data.php
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. $data

    =

    array

    (

    )

    ;

  4. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `member`"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;

  5. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  6. $data

    [

    ]

    =

    $fetch

    ;
  7. }

  8. echo

    json_encode

    (

    $data

    )

    ;

  9. ?>

script.js
Note: Make sure you save this file inside the js directory in order the script works.
  1. var

    app =

    angular.module

    (

    "myModule"

    ,

    [

    ]

    )
  2. .controller

    (

    "myController"

    ,

    function

    (

    $scope,

    $http,

    $timeout)

    {
  3. $http.get

    (

    'data.php'

    )

    .then

    (

    function

    (

    response)

    {
  4. $scope.members

    =

    response.data

    ;
  5. }

    )

    ;

  6. $scope.insetDats

    =

    function

    (

    )

    {
  7. if

    (

    $scope.firstname

    ==

    null

    ||

    $scope.lastname

    ==

    null

    ||

    $scope.address

    ==

    null

    )

    {
  8. alert(

    "Please complete the required field"

    )

    ;
  9. }

    else

    {
  10. $http.post

    (

    "insert.php"

    ,

    {

    firstname:

    $scope.firstname

    ,

    lastname:

    $scope.lastname

    ,

    address:

    $scope.address

    }

    )
  11. .then

    (

    function

    (

    )

    {
  12. $scope.firstname

    =

    ""

    ;
  13. $scope.lastname

    =

    ""

    ;
  14. $scope.address

    =

    ""

    ;
  15. $scope.getData

    (

    )

    ;
  16. }

    )

    ;
  17. }

  18. }

  19. $scope.getData

    =

    function

    (

    )

    {
  20. $http.get

    (

    'data.php'

    )

    .then

    (

    function

    (

    response)

    {
  21. $scope.members

    =

    response.data

    ;
  22. }

    )

    ;
  23. }
  24. }

    )

    ;

There you have it we successfully created a Simple Insert MySQLi Data using AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top