отображение сообщения об ошибке проверки в angularjs php - PullRequest
0 голосов
/ 26 апреля 2018

В бэкэнде я проверяю, чтобы убедиться, что переменная имени пользователя php не пуста и что переменная имени такая же, в противном случае пусть она возвращает сообщение об ошибке.Мой вид спереди - angularjs.

Проблема в том, что моя проверка данных не возвращает сообщение об ошибке.

Я реализовал проверку проверки в angularjs как

if (response.data.trim() === 'data_empty') {

//show error msg
            $scope.errorMsg = true;
            //hide the msg after 5 secs
            $timeout(function(){
$scope.errorMsg = false;
//window.location = 'errorpage.php';
}, 5000);

}


if (response.data.trim() === 'name_not_the_same') {

//show error msg
            $scope.errorMsg2 = true;
            //hide the msg after 5 secs
            $timeout(function(){
$scope.errorMsg2 = false;
}, 5000);

}

в html-файле

<div class="alert alert-danger" ng-show="errorMsg">
        <strong>Wrong!</strong> All Field Must be Filled.!.
    </div>


<div class="alert alert-danger" ng-show="errorMsg2">
        <strong>Wrong!</strong> Name must be the same.!.
    </div>

Как проверить данные и вернуть сообщения в angularjs.ниже весь код

<!doctype html>
<html>
    <head>


        <title></title>
        <link href="style.css" type="text/css" rel="stylesheet" />
        <style>
            .comment{
                border-bottom: 1px solid gray;
                padding: 5px;
            }
        </style>

    </head>
    <body ng-app='myapp'>
        <div class="content" ng-controller='fetchCtrl' >

            <div class="post" ng-repeat='post in posts'>
                <h1 >{{ post.title }}</h1>
                <div class="post-text">
                    {{ post.content }}
                </div>

            </div>

        </div>



<div class="alert alert-danger" ng-show="errorMsg">
        <strong>Wrong!</strong> All Field Must be Filled.!.
    </div>


<div class="alert alert-danger" ng-show="errorMsg2">
        <strong>Wrong!</strong> Name must be the same.!.
    </div>

        <!-- Script -->
        <script src="angular.min.js"></script>
        <script>
        var fetch = angular.module('myapp', []);
        fetch.controller('fetchCtrl', ['$scope', '$http','$timeout', function ($scope, $http, $timeout) {

                // Fetch post data
                $scope.getPosts = function(){

                    $http({
                        method: 'post',
                        url: 'likeunlike.php',
                        data: {request: 1}
                    }).then(function successCallback(response) {

$scope.posts = response.data;



if (response.data.trim() === 'data_empty') {

//show error msg
            $scope.errorMsg = true;
            //hide the msg after 5 secs
            $timeout(function(){
$scope.errorMsg = false;
//window.location = 'errorpage.php';
}, 5000);

}


if (response.data.trim() === 'name_not_the_same') {

//show error msg
            $scope.errorMsg2 = true;
            //hide the msg after 5 secs
            $timeout(function(){
$scope.errorMsg2 = false;
}, 5000);

}


                    });

                }

                $scope.getPosts(); // Fetch post data

            }


        ]);

        </script>
    </body>
</html>

php

<?php

include "config.php";

$data = json_decode(file_get_contents("php://input"));

$request = $data->request;
$userid = '';
$name ='tony';

if($userid !=''){
$response ='data_empty';
echo  $response;
exit();
}


if($name !='tony'){
$response1 ='name_not_the_same';
echo  $response1;
exit();
}




// Get all posts list and like unlike
if($request == 1){

    $response_arr = array();

    $query = "SELECT * FROM posts";
    $result = mysqli_query($con,$query);

    while($row = mysqli_fetch_array($result)){
        $postid = $row['id'];
        $title = $row['title'];
        $content = $row['content'];
        $type = -1;


        $response_arr[] = array("id" => $postid, "title" => $title);
    }

    echo json_encode($response_arr);
    exit;
}

1 Ответ

0 голосов
/ 26 апреля 2018

Похоже, вам нужно убедиться, что анализ JSON сработал первым:

$data = json_decode(file_get_contents("php://input"));
if (json_last_error() !== JSON_ERROR_NONE) { 
  echo 'bad_json';
  exit();
}

// now safe to use $data
$request = $data->request;
...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...