javascript - 使用 PHP 和 MySql 上传带有表单数据的 AngularJS 文件

标签 javascript php mysql angularjs

我正在创建一个有很多字段的表单。我担心的是我想上传 10kb jpg 文件以及其他 HTML 表单数据。

根据我的搜索,我没有看到任何解释 AngulaJS 文件上传以及其他数据的帖子。请帮助我做同样的事情。在 Angular Controller 中创建对象时,我无法获取文件上传路径,这就是它显示未定义的原因。代码如下:-

PS - 由于时间不够,我使用了表格标签。

HTML Code

<div id="wrapper">
<form>
<table>
    <thead>
        <tr>
            <td>Add Record</td>
            <td style="text-align:right"><a href="#/list" style="color:#000;">Back</a></td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Name</td>
            <td><input type="text" value="" id="name" tabindex="1" placeholder="Enter Name" ng-model="person.name" name="name"  /> </td>
        </tr>
        <tr>
            <td>Extn</td>
            <td><input type="text" value="" id="extn" tabindex="2" placeholder="Enter Extn" ng-model="person.extn" name="extn"   /> </td>
        </tr>
        <tr>
            <td>Designation</td>
            <td><input type="text" value="" id="designation" tabindex="3" placeholder="Enter Designation" ng-model="person.designation" name="designation"   /> </td>
        </tr>
        <tr>
            <td>mobile</td>
            <td><input type="text" value="" id="mobile" tabindex="4" placeholder="Enter Mobile" ng-model="person.mobile" name="mobile"   /> </td>
        </tr>
        <tr>
            <td>email</td>
            <td><input type="text" value="" id="email" tabindex="5" placeholder="Enter Email" ng-model="person.email" name="email"   /> </td>
        </tr>
        <tr>
            <td>upload file</td>
            <td><input  type="file" id="uploadFile" name="uploadFile" tabindex="6" ng-mode="person.fileUpload"   /> </td>
        </tr>
        <tr>
            <td></td>
            <td><input type="button" value="Submit" id="submitbtn" tabindex="7" ng-click="save(person)"/> </td>
        </tr>
    </tbody>
</table>
</form>

AngularJS

angular
.module("addressBook",['ngRoute'])
.controller('addController', ['$scope','$location','$rootScope','$http', function(scope,location,rootScope,http){

    scope.save = function (item){
        var personData = {
            'name' : scope.person.name, 
            'extn' : scope.person.extn, 
            'designation' : scope.person.designation, 
            'mobile' : scope.person.mobile, 
            'email' : scope.person.email,
            'fileUpload' : scope.person.fileUpload
        };

        if(typeof rootScope.crew === 'undefined'){
            rootScope.crew = [];
        }
        debugger;
        console.log(personData);
        http.post("server/insert.php",personData).success(function(data, status, headers, config){              
            scope.person = personData;
            //rootScope.crew.push(scope.person);
            location.path("/admin");
        })

    }
}]);

PHP Code

<?php
    include 'conn.php';
    $data = json_decode(file_get_contents("php://input"));

    $name = mysql_real_escape_string($data->name);
    $extn = mysql_real_escape_string($data->extn);
    $designation = mysql_real_escape_string($data->designation);
    $mobile = mysql_real_escape_string($data->mobile);
    $email = mysql_real_escape_string($data->email);


    echo $query = "INSERT INTO employee (`name`,`extn`,`designation`,`mobile`,`email`) VALUES ('$name', '$extn', '$designation', '$mobile' , '$email')";
    mysql_query($query);
    Print "Your information has been successfully added to the database.";  
?> 

最佳答案

您可以使用轻量级 Angular 指令上传带有输入字段的文件 https://github.com/danialfarid/ng-file-upload

关于javascript - 使用 PHP 和 MySql 上传带有表单数据的 AngularJS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44383090/

相关文章:

javascript - Hammer.js 平移事件仅适用于触摸设备,不适用于桌面计算机单击+拖动

PHP for 循环不显示正确的数据

javascript - 在 Question2Answer 中特定帖子的第 n 段后添加 javascript

mysql - 使用 MySQL Workbench 安全组通过 EC2 实例连接到 Amazon RDS 实例

php - 如何在使用jquery检查复选框时更新数据库?

javascript - 按键名过滤 JSON 数据

javascript - 如何使 Canvas 上的对象在移动设备上顺畅地左右移动到 x 轴

php - 从 php 中的 mysql 查询获取前一行

php - 当条件设置时,Android fcm接收来自mysql的通知

javascript - 使用所选元素将滚动条移动到顶部