在angularjs php中显示错误验证消息

时间:2018-04-25 22:09:38

标签: php angularjs

在后端,检查以确保php变量userid不为空且该名称变量相同,否则让它返回错误消息。我的前视图是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 am

<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 :(得分:0)

看起来您需要先验证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;
...
相关问题