我正在尝试使用带有angularjs的$ http.post将数据保存在数据库中。 数据在db中保存,但我无法跟踪其成功响应。
以下是我的HTML代码。
<form ng-controller="studentCntrl" name="add_user">
<div class="modal-header">
<h3 class="modal-title">Add a user by sending an invite via e-mail</h3>
</div>
<div class="modal-body">
<input type="text" class="form-control" name="user_email" ng-model="user_name" placeholder="Enter a name">
<br />
<input type="text" class="form-control" name="user_name" ng-model="user_email" placeholder="Enter an e-mail adress">
</div>
<div class="modal-footer">
<!-- <button type="" class="btn btn-success" ng-click="add_user()">Invite</button> -->
<input type="button" class="btn btn-success" name="add_user" value="Invite" ng-click="save_user()">
<button class="btn btn-warning">Cancel</button>
</div>
</form>
app.js
var app = angular.module("student_app", []);
app.controller("studentCntrl", function($scope, $http){
$scope.save_user = function() {
$http.post("insert.php", {'user_name':$scope.user_name,'user_email':$scope.user_email}).success(function(data,status){
console.log(data);
console.log("Data Inserted Successfully");
});}});
insert.php
<?php
include('db.php');
switch($_GET['action']) {
case 'add_user' :
add_user();
break;
}
function add_user() {
$data = json_decode(file_get_contents("php://input"));
$user_name = $data->user_name;
$user_email = $data->user_email;
$qry = mysql_query('INSERT INTO register(name, user_email) VALUES ("' . $user_name . '","' . $user_email . '")');}?>
我不知道我做错了什么。 希望你们能帮助我。 感谢..
答案 0 :(得分:0)
在 insert.php 文件中,在将数据插入数据库后添加以下行。
if($qry){
echo "success";
}else{
echo "Something went wrong";
}