article

Tuesday, February 8, 2022

AngularJS PHP Mysql Submit Form Data

AngularJS PHP Mysql Submit Form Data

Bootstrap 5.1 Version
https://getbootstrap.com/docs/5.1/getting-started/introduction/

angularjs CDN
https://angularjs.org/ Version 1.8.2 https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js

CREATE TABLE `members` (
  `id` int(11) NOT NULL,
  `firstname` varchar(30) NOT NULL,
  `lastname` varchar(30) NOT NULL,
  `address` varchar(150) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `members`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `members`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//index.html
<!DOCTYPE html>
<html>
<head>
<title>AngularJS PHP Mysql Submit Form Data</title>
</head>
<body>
<div class="container" style="width:750px;">
    <h3 align="center">AngularJS PHP Mysql Submit Form Data</h3>
    <br /><br />
    <div ng-app="myapp" ng-controller="formcontroller">
    <form name="userForm" ng-submit="insertData()">
        <label class="text-success" ng-show="successInsert">{{successInsert}}</label>
        <div class="form-group">
            <label>First Name <span class="text-danger">*</span></label>
            <input type="text" name="first_name" ng-model="insert.first_name" class="form-control" />
            <span class="text-danger" ng-show="errorFirstname">{{errorFirstname}}</span>
        </div>
        <div class="form-group">
            <label>Last Name <span class="text-danger">*</span></label>
            <input type="text" name="last_name" ng-model="insert.last_name" class="form-control" />
            <span class="text-danger" ng-show="errorLastname">{{errorLastname}}</span>
        </div>
        <div class="form-group">
            <label>Address <span class="text-danger">*</span></label>
            <input type="text" name="address" ng-model="insert.address" class="form-control" />
            <span class="text-danger" ng-show="erroraddress">{{erroraddress}}</span>
        </div>
        <br />
        <div class="form-group">
            <input type="submit" name="insert" class="btn btn-info" value="Insert"/>
        </div>
    </form>
    </div>
</div>
   
<script>
var application = angular.module("myapp", []);
application.controller("formcontroller", function($scope, $http){
    $scope.insert = {};
    $scope.insertData = function(){
        $http({
            method: 'POST',
            url: 'insert.php',
            data:$scope.insert,
        }).then(function (data){
            console.log(data)
               if(data.data.error)
               {
                $scope.errorFirstname = data.data.error.first_name;
                $scope.errorLastname = data.data.error.last_name;
                $scope.erroraddress = data.data.error.address;
                $scope.successInsert = null;
               }
               else
               {
                $scope.insert = null;
                $scope.errorFirstname = null;
                $scope.errorLastname = null;
                $scope.erroraddress = null;
                $scope.successInsert = data.data.message;
               }
        },function (error){
            console.log(error, 'can not post data.');
        });
    }
});
</script>
</body>
</html>
insert.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//insert.php
<?php
$connect = mysqli_connect("localhost", "root", "", "testingdb");
$form_data = json_decode(file_get_contents("php://input"));
$data = array();
$error = array();
 
if(empty($form_data->first_name))
{
    $error["first_name"] = "First Name is required";
}
 
if(empty($form_data->last_name))
{
    $error["last_name"] = "Last Name is required";
}
 
if(!empty($error))
{
    $data["error"] = $error;
}
else
{
    $first_name = mysqli_real_escape_string($connect, $form_data->first_name);
    $last_name = mysqli_real_escape_string($connect, $form_data->last_name);
    $address = mysqli_real_escape_string($connect, $form_data->address);
    $query = "
      INSERT INTO members(firstname, lastname, address) VALUES ('$first_name', '$last_name', '$address')
    ";
    if(mysqli_query($connect, $query))
    {
      $data["message"] = "Data Inserted...";
    }
}
echo json_encode($data);
?>

Related Post