donv29560 2014-09-15 10:37
浏览 29

将动态创建的输入从Angular提交到PHP页面

I am trying to create an invoice form, which can make all the necessary calculations like subtotal, tax total by itself .

Hitting the submit button should submit all values and the dynamically created items to a PHP page, which will insert these values (written by the user, or calculated by angularjs) to the appropriate SQL table/column as I wish.

Based on this project , I added this javascript code.

function PhpCtrl($scope, $http, $templateCache) {
var method = 'POST';
var url = 'added.php';
$scope.codeStatus = "";
$scope.add = function() {
var FormData = {};
$http({
  method: method,
  url: url,
  data: FormData,
  headers: {'Content-Type': 'application/x-www-form-urlencoded'},
  cache: $templateCache
}).
success(function(response) {
    $scope.codeStatus = response.data;
}).
error(function(response) {
    $scope.codeStatus = response || "Request failed";
});
return false;
};
}

Can anyone help me how to store all the dynamically created items, along with their values, in order to submit them to a php page ?

  • 写回答

1条回答 默认 最新

  • dongpuchao1680 2014-09-15 10:46
    关注

    You can create a FormData to be submitted dynamically using plain javascript like this:

    var fd = new FormData();
    
    fd.append("fieldName", valueToSubmit);
    fd.append("arrayField[]",[1,2,3]); //you can even post array data
    

    The you push fd to the server:

    var uri = "added.php";
    var xhr = new XMLHttpRequest();
    xhr.open("POST",uri,true);
    xhr.onreadystatechange=function(){
        if(xhr.readyState == 4 && xhr.status==200){
            //do something with returned data
        }
    };
    fd.append("img_file",blob_to_upload);// you can even upload a file
    fd.append("user_id",data);
    fd.append("category_id",category_id);
    xhr.send(fd);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题