AngularJs http post Request To PHP json

Today, We want to share with you AngularJs http post Request To PHP json.In this post we will show you how to send data from angularjs to php, hear for AngularJs Simple Example: Post Data to PHP page we will give you demo and example for implement.In this post, we will learn about how to call php function from angularjs controller with an example.

AngularJs http post Request To PHP json

There are the Following The simple About AngularJs http post Request To PHP json Full Information With Example and source code.

As I will cover this Post with live Working example to develop Posting Form Data With $http In AngularJS, so the How to Submit Form with Validation in AngularJS with PHP for this example is following below.

Step 1: HTML Part

index.html

<!DOCTYPE html>
<html ng-app>
    <head>
        <title>AngularJs Post Example: pakainfo.com </title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
        <style>
            #dv1{
                border:1px solid #DBDCE9; margin-left:auto;
                margin-right:auto;width:220px;
                border-radius:7px;padding: 25px;
            }

            .info{
                border: 1px solid;margin: 11px 0px;
                padding:11px;color: #00529B;
                background-color: #BDE5F8;list-style: none;
            }
            .err{
                border: 1px solid;  margin: 11px 0px;
                padding:11px;  color: #D8000C;
                background-color: #FFBABA;   list-style: none;
            }
        </style>
    </head>
    <body>
        <div id='dv1'>
            <form ng-controller="productController">
                <ul>
                    <li class="err" ng-repeat="error in display_err"> {{ error}} </li>
                </ul>
                <ul>
                    <li class="info" ng-repeat="r_message in r_messages"> {{ r_message}} </li>
                </ul>
                <h2>Product Submit Form</h2>
                <div>
                    <label>Product Name</label> 
                    <input type="text" ng-model="livename" placeholder="Product Name" style='margin-left: 22px;'> 
                </div>
                <div>
                    <label>Shop Key</label>
                    <input type="text" ng-model="liveshop_key" placeholder="Shop Key" style='margin-left: 22px;'> 
                </div>
                <div>
                    <label>product auth Token</label>
                    <input type="password" ng-model="product_auth" placeholder="product_auth">
                </div>
                <button ng-click='productSubmit();' style='margin-left: 63px;margin-top:10px'>productSubmit</button>
            </form>
        </div>

        <script type="text/javascript">
            function productController($scope, $http) {
                $scope.display_err = [];
                $scope.r_messages = [];

                $scope.productSubmit = function() {

                    $scope.display_err.splice(0, $scope.display_err.length); // delete all error messages
                    $scope.r_messages.splice(0, $scope.r_messages.length);

                    $http.post('post_es.php', {'pname': $scope.livename, 'product_auth': $scope.product_auth, 'shop_key': $scope.liveshop_key}
                    ).success(function(data, status, headers, config) {
                        if (data.r_message != '')
                        {
                            $scope.r_messages.push(data.r_message);
                        }
                        else
                        {
                            $scope.display_err.push(data.error);
                        }
                    }).error(function(data, status) { 
                        $scope.display_err.push(status);
                    });
                }
            }
        </script>
        <a href='https://www.pakainfo.com'>pakainfo.com</a>
    </body>
 </html>

Step 2: PHP Code

simple PHP source Code will be:

<?php

$data = json_decode(file_get_contents("php://input"));
$product_name = mysql_real_escape_string($data->pname);
$product_auth = mysql_real_escape_string($data->product_auth);
$shop_key = mysql_real_escape_string($data->shop_key);

$con = mysql_connect('localhost', 'root', '');
mysql_select_db('pakainfo', $con);

$qry_em = 'select count(*) as cnt from products where shop_key ="' . $shop_key . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);

if ($res['cnt'] == 0) {
    $qry = 'INSERT INTO products (name,pass,shop_key) values ("' . $product_name . '","' . $product_auth . '","' . $shop_key . '")';
    $qry_res = mysql_query($qry);
    if ($qry_res) {
        $my_res_array = my_res_arrayay('r_message' => "Product Created Successfully!!!", 'error' => '');
        $results_json = json_encode($my_res_array);
        print_r($results_json);
    } else {
        $my_res_array = my_res_arrayay('r_message' => "", 'error' => 'Error In inserting record');
        $results_json = json_encode($my_res_array);
        print_r($results_json);
    }
} else {
    $my_res_array = my_res_arrayay('r_message' => "", 'error' => 'Product Already exists with same shop_key');
    $results_json = json_encode($my_res_array);
    print_r($results_json);
}
?>

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about AngularJs http post Request To PHP json.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Laravel Calculate multiple totals in conditional aggregates
  2.   AngularJS Add and Remove Comments system
  3.   Laravel 6 Eloquent ORM Delete Example
  4.   Custom Service using Angular Example
  5.   How to display cross domain json data using Jquery
  6.   Http Post Method using Angular Example
  7.   Simple Jquery Scroll To Top Example
  8.   PHP MySQL Inline Editing Dynamically using jQuery Ajax
  9.   Everything You Need to Know About Date format in PHP
  10.   Laravel Get Last 7 days month year record from MySQL