2013-11-15 6 views
15

में फ्रंट एंड से mysql डीबी में डेटा डालने से मैं angularjs का उपयोग कर फ्रंट एंड से mysql डीबी तक डेटा डालने का प्रयास कर रहा हूं। लेकिन यह डीबी में डाला नहीं जा रहा है भले ही कोई त्रुटि संदेश नहीं है। निम्नलिखित कोड है जिसका मैं उपयोग करता हूं।एंजुलरज

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> 

और पीछा कर रहा है मेरी php फ़ाइल

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."; 
    } 
?> 

मैं जानता हूँ कि मैं यहाँ कुछ बेवकूफ कर रहा हूं। मैंने आज angularjs सीखना शुरू कर दिया है। जब मैं डीबी में डालने के लिए सादा एचटीएमएल के साथ PHP कोड का प्रयास करता हूं तो यह पूरी तरह से काम करता है। मुझे यह नहीं मिल रहा है कि मैं यहां क्या कर रहा हूं। उम्मीद है कि कोई मेरी मदद करेगा

+0

यदि php कोणीय के बिना काम कर रहा है, तो हम इसे कारण के रूप में छूट सकते हैं, लेकिन आपको mysql_ * कार्यों के बजाय mysqli_ * या PDO का उपयोग करना चाहिए। क्रोम डेवलपर टूल का उपयोग करना, या इसी तरह, क्या आप एक्सएचआर को अपनी स्क्रिप्ट में पोस्ट कर सकते हैं? क्या इसका "एड" के लिए मूल्य है? मैं नहीं देख सकता कि आप पोस्ट "एड" मान कहां सेट कर रहे हैं, लेकिन मैं कोणीय से अपरिचित हूं। Mysql_ * का उपयोग न करने के बारे में अधिक जानकारी यहां मिल सकती है [http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – boodle

+1

क्या आपको कॉलबैक मिलता है या अनुरोध कोड के लिए कम से कम कुछ प्रतिक्रिया कोड? –

+0

निश्चित रूप से ऐसा लगता है कि आप एक _POST ["add"] मान नहीं भेज रहे हैं, और इसलिए if में प्रवेश नहीं कर रहे हैं। – boodle

उत्तर

17

डेटा डालने के लिए आपकी स्क्रिप्ट गलत है। इसे

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

और निम्नानुसार php को भी बदलें।

$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."; 

जब मैंने आपके कोड के साथ प्रयास किया तो यह मेरे लिए काम करता था।

+2

यह यहां भी काम करता है :) – StreetCoder

संबंधित मुद्दे