簡體   English   中英

Angular.js將表單輸入保存到php和Mysql

[英]Angularjs save form input to php and Mysql

我在嘗試學習和構建angularjs應用程序時遇到問題。 我只是無法將表單數據保存到mysql ...我花了數小時嘗試了所有事情,但它根本無法正常工作。

使用該變量時,PHP文件將空查詢輸入到mysql表。 如果我使用靜態信息,則效果很好。

謝謝 :)

 app.controller('addMsgCtrl', function ($scope, $http) { $scope.fill = function(add) { var data = { addItem: $scope.addItem }; $http.post("msinput.php",{'data' : $scope.addItem}) .success(function(data, status, headers, config){ console.log("inserted Successfully"); }); }; }); 
 $data = json_decode(file_get_contents("php://input")); $addItem = mysql_real_escape_string($data->addItem); mysql_connect("localhost", "username", "password") or die(mysql_error()); mysql_select_db("stran189_taskma") or die(mysql_error()); mysql_query("INSERT INTO message (addItem) VALUES ('$addItem')"); Print "Your information has been successfully added to the database."; 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <form class="add-task" id="newTaskForm" ng-controller="addMsgCtrl" novalidate ng-submit="fill()"> <div class="form-actions"> <div class="input-group"> <input ng-model="addItem" class="form-control" type="text" name="addItem" placeholder="Ny anteckning" /> <div class="input-group-btn"><button class="btn btn-default" type="submit"> <i class="glyphicon glyphicon-plus"></i></button> </div> </div> </div> </form> 

JS :“已成功插入!” NET :“ POST 200 OK msinput.php” 請求標頭 :“主機:(myUrl)用戶代理:Mozilla / 5.0(Macintosh; Intel Mac OS X 10.11; rv:44.0)Gecko / 20100101 Firefox / 44.0接受:application / json ,文本/純文本/ /接受語言:en-US,en; q = 0.5接受編碼:gzip,deflate引用者:(myUrl)內容類型:application / json; charset = utf-8內容長度:2連接: 活着”

響應標題 :連接:保持活動內容長度:62內容類型:text / html日期:星期一,2015年12月7日21:36:53 GMT保持活動:超時= 5,最大= 100服務器:Apache / 2.4 .10(Unix)OpenSSL / 0.9.8e-fips-rhel5 mod_bwlimited / 1.4變體:User-Agent X-Powered-By:PHP / 5.4.37

我對您的控制器代碼有些困惑,但是在我看來,您已經准備好要發送到服務器的var數據。 如果是這樣,則必須$ http.post(“ msinput.php”,{'data':$ scope.data})...

在您的fill()日志數據中。 看看里面是否有任何東西。 改變這個

$http.post("msinput.php",{'data' : $scope.addItem})

對此

$http.post("msinput.php",{'data' : data})

然后檢查您的Network XHR,查看是否將數據正確發送到服務器。

另外,要獲取有關服務器回傳內容的更多信息,您還可以使用此方法並記錄錯誤:

// Simple GET request example:
$http({
  method: 'POST',
  url: 'msinput.php'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
 }, function errorCallback(response) {
      // called asynchronously if an error occurs
      // or server returns response with an error status.
});

首先確保您的數據存在:

在Firefox中打開Firebug,選擇控制台標簽

在Firefox中導航到您的表單,填寫表單並提交。

控制台的最后一行應顯示:

POST http://..../msinput.php

單擊此按鈕,然后查看POST和JSON選項卡,它將顯示每個字段的所有輸入數據。 缺少的任何東西顯然都不會進入您的數據庫。

一旦知道您的數據即將離開,您就可以找出為什么它沒有到達的原因。

我也強烈建議您更改為PDO並停止使用mysql函數(如果現在已棄用)。

好的,現在您知道表單中的數據將通過發布操作保留。

在您的msinput.php內部執行以下操作

var_dump( file_get_contents(http://input) );

查看Firebug控制台返回的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM