Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
60
rated 0 times [  62] [ 2]  / answers: 1 / hits: 48598  / 11 Years ago, fri, november 15, 2013, 12:00:00

I am trying to insert data from front end to mysql db using angularjs. But it is not geting inserted to the db even though there are no error messages. Following is the code that I use.



index.html



<html ng-app=demoApp>
<head>
<title> AngularJS Sample</title>
<script src=js/angular.min.js></script>
<script src=js/angular-route.min.js></script>
<script type=text/javascript src=js/script.js></script>
</head>
<body>
<div class=container ng-view>
</div>
</body>
</html>


script.js



    demoApp.config( function ($routeProvider,$locationProvider) {
$routeProvider
.when('/',
{
controller: 'SimpleController',
templateUrl: 'Partials/view1.html'
})
.when('/view2',
{
controller: 'SimpleController',
templateUrl: 'Partials/view2.html'
})
.otherwise({redirectTo: '/'});
});
demoApp.controller('SimpleController',function ($scope,$http){
$http.post('server/view.php').success(function(data){
$scope.friends = data;
});;
$scope.addNewFriend = function(add){
var data = {
fname:$scope.newFriend.fname,
lname:$scope.newFriend.lname
}
$http.post(server/insert.php,data).success(function(data, status, headers, config){
console.log(inserted Successfully);
});
$scope.friends.push(data);
$scope.newFriend = {
fname:,
lname:
};
};
});


View1.html



<div class=container style=margin:0px 100px 0px 500px;>
Name:<input type=text ng-model=filter.name>
<br/>
<ul>
<li ng-repeat=friend in friends | filter:filter.name | orderBy:'fname'>{{friend.fname}} {{friend.lname}}</li>
</ul>
<br/>
<fieldset style=width:200px;>
<legend>Add Friend</legend>
<form name=addcustomer method=POST>
First Name:<input type=text ng-model=newFriend.fname name=firstname/>
<br/>
Last Name :<input type=text ng-model=newFriend.lname name=lastname/>
<br/>
<button data-ng-click=addNewFriend() name=add>Add Friend</button>
</form>
</fieldset>
<a href=#/view2 style=margin:auto;>Next</a>
</div>


and following is my php file



insert.php



<?php
if(isset($_POST['add']))
{
$firsname=$_POST['firstname'];
$laname = $_POST['lastname'];
mysql_connect(localhost, root, ) or die(mysql_error());
mysql_select_db(angularjs) or die(mysql_error());
mysql_query(INSERT INTO friends (fname,lname) VALUES ('$firsname', '$laname'));
Print Your information has been successfully added to the database.;
}
?>


I know I am doing something stupid here. I have just started to learn angularjs today.
when i try the php code with plain html to insert into db it works perfectly.I am not getting what I am doing wrong here. Hope someone will help me out here


More From » php

 Answers
18

Your script for inserting the data is wrong. Replace it with the following



$http.post(server/insert.php,{'fstname': $scope.newFriend.fname, 'lstname': $scope.newFriend.lname})
.success(function(data, status, headers, config){
console.log(inserted Successfully);
});


and also change the php as follows.



$data = json_decode(file_get_contents(php://input));
$fstname = mysql_real_escape_string($data->fstname);
$lstname = mysql_real_escape_string($data->lstname);
mysql_connect(localhost, root, ) or die(mysql_error());
mysql_select_db(angularjs) or die(mysql_error());
mysql_query(INSERT INTO friends (fname,lname) VALUES ('$fstname', '$lstname'));
Print Your information has been successfully added to the database.;


This worked for me when I tried with your code.


[#74264] Wednesday, November 13, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrence

Total Points: 120
Total Questions: 115
Total Answers: 87

Location: England
Member since Fri, May 22, 2020
4 Years ago
terrence questions
Sat, Jun 5, 21, 00:00, 3 Years ago
Wed, Jun 17, 20, 00:00, 4 Years ago
;