php angular使用,如何使用angular.js PHP从mysql显示数据?

本文介绍了一个使用AngularJS前端框架与PHP后端语言相结合的用户注册功能实现案例。前端通过AngularJS发送POST请求到服务器,而后端则处理这些请求,完成用户的注册流程,并返回相应的结果给前端。

您好,您可以查看以下示例代码

你的HTML页面:

AngularJs Post Example: DevZone.co.in

#dv1{

border:1px solid #DBDCE9; margin-left:auto;

margin-right:auto;width:220px;

border-radius:7px;padding: 25px;

}

.info{

border: 1px solid;margin: 10px 0px;

padding:10px;color: #00529B;

background-color: #BDE5F8;list-style: none;

}

.err{

border: 1px solid; margin: 10px 0px;

padding:10px; color: #D8000C;

background-color: #FFBABA; list-style: none;

}

  • {{ error}}
  • {{ msg}}

Sigup Form

Name

Email

Password

SignUp

function FrmController($scope,$http) {

$scope.errors = [];

$scope.msgs = [];

$scope.SignUp = function() {

$scope.errors.splice(0,$scope.errors.length); // remove all error messages

$scope.msgs.splice(0,$scope.msgs.length);

$http.post('post_es.PHP',{'uname': $scope.username,'pswd': $scope.userpassword,'email': $scope.useremail}

).success(function(data,status,headers,config) {

if (data.msg != '')

{

$scope.msgs.push(data.msg);

}

else

{

$scope.errors.push(data.error);

}

}).error(function(data,status) { // called asynchronously if an error occurs

// or server returns response with an error status.

$scope.errors.push(status);

});

}

}

Devzone.co.in

////////////////////////////////////////////////////////////////////////

Your PHP code

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

$usrname = MysqL_real_escape_string($data->uname);

$upswd = MysqL_real_escape_string($data->pswd);

$uemail = MysqL_real_escape_string($data->email);

$con = MysqL_connect('localhost','root','');

MysqL_select_db('test',$con);

$qry_em = 'select count(*) as cnt from users where email ="' . $uemail . '"';

$qry_res = MysqL_query($qry_em);

$res = MysqL_fetch_assoc($qry_res);

if ($res['cnt'] == 0) {

$qry = 'INSERT INTO users (name,pass,email) values ("' . $usrname . '","' . $upswd . '","' . $uemail . '")';

$qry_res = MysqL_query($qry);

if ($qry_res) {

$arr = array('msg' => "User Created Successfully!!!",'error' => '');

$jsn = json_encode($arr);

print_r($jsn);

} else {

$arr = array('msg' => "",'error' => 'Error In inserting record');

$jsn = json_encode($arr);

print_r($jsn);

}

} else {

$arr = array('msg' => "",'error' => 'User Already exists with same email');

$jsn = json_encode($arr);

print_r($jsn);

}

?>

请检查上面的代码,您将了解它将如何工作.如果您遇到任何问题,请在评论栏中告诉我

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值