Javascript 使用 AngularJS 和 php 将数据从表单保存到数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26207778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:31:42  来源:igfitidea点击:

Saving Data from form to database using AngularJS and php

javascriptphpdatabaseangularjs

提问by

I'm pretty new in angular.
i created a modal window to user share story and after that show it in body:

我在角度方面很新。
我创建了一个模式窗口来用户分享故事,然后在正文中显示它:

html

html

 <button class="btn btn-primary new-story" ng-click="showPopup()">New Story</button>


            <div class="wroteStory img-rounded" ng-repeat="story in sentCompose">

                <h1 class="s-label"><i><label for="subject">Subject :</label></i></h1>
                <h5>{{story.subject}}</h5>


                <h1 class="s-label"><i><label for="body">Full Story :</label></i></h1>
                <h6>{{story.body}}</h6>
                <button class="btn btn-danger" ng-click="remove($index)"> Delete </button>
                <hr/>
            </div>

js

js

app.controller('aboutController', function($scope,ngProgress) {


$scope.popupVisible = false;
$scope.showPopup = function(){
    $scope.popupVisible = true;
    $scope.composeStory = {};
}
$scope.closePopup = function(){
    $scope.popupVisible = false;
}


$scope.composeStory = {};
$scope.sentCompose = [];

$scope.sendStory = function() {
   $scope.sentCompose.push($scope.composeStory);
    $scope.popupVisible = false;


    $http.post("insert.php", data).success(function(data, status, headers, config){

    });

};

i want to save data from this form to database ? Thx in advance

我想将此表单中的数据保存到数据库中?提前谢谢

回答by Virtu

With limited information you provided, i will try to help you on this. When you ask questions, please show what output your are getting, what your debugging says. It will be helpful for others to understand your problem and suggest you some solution. Here is my suggestions

由于您提供的信息有限,我会尽力帮助您。当你提问时,请展示你得到的输出,你的调试结果。这将有助于其他人了解您的问题并建议您一些解决方案。这是我的建议

1) I could not see scope of your $http.post("insert.php", **data**)data variable make sure that you have serialized data in it.

1)我看不到你的$http.post("insert.php", **data**)数据变量的范围,确保你有序列化的数据。

2) Check in firebug whether your request is being sent or not. If you can see the request then see what is the response you are getting

2)检查萤火虫是否正在发送您的请求。如果您可以看到请求,请查看您得到的响应是什么

3) As you have success handler always have a error handler for any Ajax call, it is a best practice and saves lot of your time in debugging

3) 因为你有成功处理程序总是有任何 Ajax 调用的错误处理程序,这是一个最佳实践,可以节省大量调试时间

My suggestions are based on assumption that your insert.phpis tested for inserting data properly. If not you have to follow what John Conde told

我的建议是基于这样的假设,即您insert.php已经过正确插入数据的测试。如果不是,您必须遵循约翰康德所说的

回答by Sadeghbayan

first you should be able to create a db in mysql and creat a table for that .
then you should be able to connect them with php like this :

首先,您应该能够在 mysql 中创建一个 db 并为此创建一个表。
那么你应该能够像这样用php连接它们:

    $host = "localhost";
$user = "angular";
$pass = "angular";
$database = "angular";
$con = mysql_connect($host,$user,$pass);
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db($database,$con);