Want to invoke PHP using AngularJS

Asked 2 years ago, Updated 2 years ago, 50 views

Currently, I am trying to use AngularJS to call PHP and connect to Mysql to create HTML display.
As a result of the query, it seems that none of them have been extracted

Place the sauce

main.html

<!DOCTYPE html>
<html lang="ja">
    <head>
        <title>sample</title>
        <metacharset="UTF-8">

        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-resource.min.js"></script>
        <script src="C:\xampp\htdocs\PHP\controller.js"></script>
    </head>

    <bodyng-app="mainApp"ng-controller="MainCtrl">
        <h2>/h2>
        <div>

        <table border="1">
            <tr>
                <th>/th>
                <th>Name</th>
                <th>Age</th>
                <th>ID</th>
            </tr>
            <trng-controller="DetailCtrl"ng-repeat="employee in employees">
                <td>{{employee.id}}</td>
                <td><inputng-model="employee.name">/td>
                <td><inputng-model="employee.age">/td>
                <td><inputng-model="employee.id">/td>
                <td><button ng-click="update()">Update;/button></td>
                <td><buttonng-click="delete()">delete>/button></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><inputng-model="new_student.name">/td>
                <td><inputng-model="new_student.age">/td>
                <td><inputng-model="new_student.id">/td>
                <td><button ng-click="add()">Add</button></td>
                <td>&nbsp;</td>
            </tr>
        </table>

    </body>
</html>

controller.js

varmainApp=angular.module("mainApp", "ngResource"));

mainApp.controller("MainCtrl", function($scope,$resource,$window){


    // Define PHP to invoke
    varEmployee=$resource('dbController.php', {id:'@id'});
    // Storing sample table records from dbController.php to $scope.employees
    $scope.employees=Employee.query();
/*  $scope.employees={
        info: [
            {name:'aaa', age:'21', id:'111'},
            {name:'bbb', age:'21', id:'222'},
            {name: 'ccc', age: '22', id: '333'}
        ] 
    }*/
    // When the Add button is pressed
    $scope.add=function(){
        // Insert PHP invocation record at POST
        Employee.save($scope.new_employee, function(){
            alert(" Added.");
            // Reload Screen
            $window.location.reload();
        });
    };

});

mainApp.controller('DetailCtrl', function($scope,$window){
    $scope.update=function(){
        $scope.employee.$save(function(){
            alert("Updated.");
        });
    };
    $scope.delete=function(index){
        $scope.employee.$delete();
        alert("Deleted.");
        $window.location.reload();
    };
});

dbController.php

<?php 
//phpinfo();
$host='localhost';
$dbname = 'mysql';
$user='root';
$pass = 'pass';

try{
    $mysqli = newmysqli($host,$user,$pass,$dbname);

    if($mysqli->connect_errno){
        printf("Connect failed:%s\n", $mysqli->connect_error);
        exit();
    }

    switch($_SERVER['REQUEST_METHOD'){
        case 'GET':
            $sql = "select * from sample";
            $result=$mysqli->query($sql);

            if(mysqli_num_rows($result)){
                while($row=mysqli_fetch_assoc($result)){
                    $arr[] = $row;
                }
            }

            echo$json_info=json_encode($arr);
            break;
        case 'POST':
            $in = json_decode(file_get_contents('php://input',ure);
            if(isset($in['id'])){
            // if(isset($_GET['id'])){
                $sql="UPDATE emp SET name=?,age=? where id=?";
                $stmt = $mysqli->prepare($sql);
                $stmt->bind_param('sss',$name,$age,$id);
                $name=isset($_REQUEST['name'])?$_REQUEST['name']: 'dummy_name';
                $name=isset($_REQUEST['age'])?$_REQUEST['age']:1;
                $name=isset($_REQUEST['id'])?$_REQUEST['id']:1;
                $stmt->execute();
            } else {
                $sql="INSERT INTO sample(name,age)VALUES(?,?)";
                $stmt = $mysqli->prepare($sql);
                $stmt->bind_param('ss',$name,$age);
                $name=isset($_REQUEST['name'])?$_REQUEST['name']: 'dummy_name';
                $name=isset($_REQUEST['age'])?$_REQUEST['age']:1;
                $stmt->execute();
            }

            break;
        case 'DELETE':
            $sql="DELETE FROM sample WHERE id=?";
            $stmt = $mysqli->prepare($sql);
            $stmt->bind_param('i',$id);
            $id = $_GET ['id'];
            $stmt->execute();
            break;
    }
}catch(PD0Exception$e){
    exit('Database connection failed.'.$e->getMessage());
}
?>

$scope.employees=Employee.query();

I put this query result in employees, but the element of putting it in is zero.
As a trial,

$scope.employees={
        info: [
            {name:'aaa', age:'21', id:'111'},
            {name:'bbb', age:'21', id:'222'},
            {name: 'ccc', age: '22', id: '333'}
        ] 
    }

When I put the data in the better, the display went well.
When I ran dbController.php as it was, I found that there were two pieces of data.

The problem is that dbController.php is not loaded.

I have written my own pass and referred to various websites, but it doesn't work.
If you understand, please let me know.

angularjs

2022-09-30 14:03

1 Answers

Error: The [$injector:unpr] issue has been resolved.

varmainApp=angular.module("mainApp", []);

It was a simple mistake to say that there was no "ngResource" in this part.

varmainApp=angular.module("mainApp", "ngResource"));

I made this mistake because I forgot to erase it myself once.


2022-09-30 14:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.