【问题标题】:json encode in php not workingphp中的json编码不起作用
【发布时间】:2017-03-26 06:43:45
【问题描述】:

这是我的代码。我想用 angular 和 php 检查验证 这是 html 。我要检查验证此表单

<form ng-submit="dk()">
<label for="">Name</label>
<input type="text" name="name" ng-model="formData.name">

{{errorName}}
    <button type="submit">Submit</button>
</form>
{{formData}}
{{message}}

这里是 angular 。我向 php 代码发送 http 请求

function dkController($scope,$http){
            $scope.formData ={};
            $scope.dk = function(){
                $http({
                    method:'POST',
                    url:'test.php',
                    data:$.param($scope.formData),

                }).then(onSuccess)

                function onSuccess(response){
                    $scope.mydata = response.data;
                    if($scope.mydata.success==false){
                        $scope.errorName = response.data.errors.name;
                    }else{
                        $scope.message = response.data.message;
                        $scope.errorName='';
                    }
                }

这里是 php 。我在这里检查代码

  <?php
    $data=array();
    $errors=array();
    $conn = mysqli_connect('localhost','root','','project'); 
    if(isset($_POST['name'])){
    $username =$_POST['name'];
    $sql="SELECT * FROM hoso where username='$username'";
    $result = mysqli_query($conn,$sql);
    if(mysqli_num_rows($result)>0){
        $errors['username']='You cant use this username';
    }
    }


    if(!empty($errors)){
        $data['success']=false;
        $data['errors']=$errors;
    }else{
        $data['success']=true;
        $data['message']='Success';
    }

    echo json_encode($data);
    ?>

这里是显示{"success":true,"message":"Success"} 我不知道为什么这个 json 没有错误。我哪里错了。请帮我 。谢谢

【问题讨论】:

  • 对我来说一切都很好。你想在这里实现什么
  • @PhpDev 我想做这样的scotch.io/tutorials/submitting-ajax-forms-the-angularjs-way。如果我检查空用户名它工作正常,但是当我检查数据库时,它不起作用
  • 您确定要检查isset($_POST['name']) 而不是$_POST['username']?如果$_POST['name'] 没有设置,它不会抛出任何错误。
  • @TomUdding 是的。我正在使用isset($_POST['name'])
  • 我的意思是;您在表单中使用nameusername 吗? (您能否将表单代码添加到问题中)

标签: php angularjs json


【解决方案1】:

首先在你的角度方法onSuccess只放下面,更新如下

function onSuccess(response){
    $scope.mydata = response.data;
}

要看看你得到了什么,请将{{message}}替换为 {{mydata}} 通过这个你可以看到发生了什么。
执行上述操作只是为了检查您从 PHP 脚本中得到了什么。以上代码不是解决方案。
而且您的角度方法 onSuccess 也有错误,请更正您的角度值,而不是 response.data.errors.name; 使用 response.data.errors.username;

完成我提到的所有更正后,请打印 {{errorName }} 并查看消息。

只需使用以下
更新: HTML

<form ng-submit="dk()">
<label for="">Name</label>
<input type="text" name="name" ng-model="formData.name">

{{errorName}}
    <button type="submit">Submit</button>
</form>
{{formData}}
{{errorName}}

JS

function onSuccess(response){
    $scope.mydata = response.data;
    if($scope.mydata.success==false){
        $scope.errorName = response.data.errors.username;
    }else{
        $scope.message = response.data.message;
        $scope.errorName='Success msg';
    }
}

【讨论】:

  • 看来你误会了。我想在输入中写入用户名时,php 将检查数据库,如果数据库中有用户名,将显示:'你不能使用这个用户名'。感谢您的帮助'
  • 是的,所以首先通过修改 onSuccess 方法并在 html 页面上用 mydata 替换 message 来检查您从 PHP 脚本中得到了什么。它只是为了测试而不是为了你的问题。我想让你看看你从你的 php 脚本中得到了什么。我已经更新了帖子。
  • 我在这里得到{"success":true,"message":"Success"}
  • 你没有$scope.errorName = response.data.errors.name;,因为你有$errors['username']='You cant use this username';。这就是为什么你必须使用$scope.errorName = response.data.errors.username; 而不是$scope.errorName = response.data.errors.name;。您看不到您将消息作为 $error['username'] 传递,但使用 errors.name 进行访问。
  • 天啊。我错了。它是formData.username 而不是formData.name。你救了我的命。非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-21
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多