【发布时间】: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']) -
我的意思是;您在表单中使用
name或username吗? (您能否将表单代码添加到问题中)