【问题标题】:submit form data and files using phonegap?使用phonegap 提交表单数据和文件?
【发布时间】:2013-10-24 17:04:06
【问题描述】:

我创建了简单的 html 表单,其中包含文本输入、文本区域、上传图像等文件..

当把它变成包含用户注册到他的数据和照片和简历文件的电话间隙应用程序时,我如何提交这个表单数据?

当提交数据时,它应该被发送到 mysql 插入的 php 页面?

我已经尝试过序列化方法在 php 页面中获取数据时遇到问题

也找不到上传文件和图片的方法??

任何帮助???

【问题讨论】:

  • 你需要粘贴一些相关代码
  • 您在哪里提交数据......外部数据库或其他?
  • 我要提交数据到服务器上传的外部mysql数据库
  • 更多信息请看这篇文章stackoverflow.com/questions/19634967/…

标签: php mysql json file-upload cordova


【解决方案1】:
  <script>

document.addEventListener("deviceready", onDeviceReady, false);

 function onDeviceReady() {

console.log("你好"); $("#button").click(function(evt){ var name = $("#name").val(); var message = $("#message").val(); var sendData = { "name": name, "message": message };

$.ajax({
    type: "POST",
    url: "http://localhost/webs/main/ajax/process.php",
    data: sendData,
    success: function(data) {
        log.console(data);
         $("#info").html(data);

    }

});

 alert("Hello ");
 alert($("#test").val());
 alert($("#name").val());
 alert($("#message").val());
 return false;

}); }

【讨论】:

  • 我看到了..ok 如何上传图片?
【解决方案2】:
Upload image using phonegap
============================

function uploadImage(){
       //Using Camera
       navigator.camera.getPicture(uploadPhoto, onFailcapturePhoto, { quality: 50,destinationType: Camera.DestinationType.FILE_URI }); 
        //Using library            
       navigator.camera.getPicture(uploadPhoto, onFailcapturePhoto, { quality: 50,destinationType: navigator.camera.DestinationType.FILE_URI, sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY});    
}

function onFailcapturePhoto(message) {    
   console.log("Message = " + message);
}

function uploadPhoto(imageURI) {
  var imagefile = imageURI; 
  $('#vImage').attr('src', imagefile);
/* Image Upload Start */
var ft = new FileTransfer();                     
var options = new FileUploadOptions();                      
options.fileKey="vImage1";                      
options.fileName=imagefile.substr(imagefile.lastIndexOf('/')+1);
options.mimeType="image/jpeg";  
var params = new Object();
params.value1 = "test";
params.value2 = "param";                       
options.params = params;
options.chunkedMode = false;                       
ft.upload(imagefile, your_service_url, win, fail, options);   
}

function win(r) {
  console.log("Code = " + r.responseCode);
   console.log("Response = " + r.response);
   //alert($.parseJSON(r.response))    
}

function fail(error) {
   console.log("Response = " +  error.code);
} 
On your php file 
=================
 file_put_contents("file.txt", print_r($_FILES, true));
 Post Form data and Image together 
 ================================ 
// Here first submit your form input data after successfully submit upload image call

$('#submit').on('click', function(event){
if(event.handled !== true)
{
    var ajax_call = serviceURL; 
    var str = $('#form').serialize();
    $.ajax({
        type: "POST",
        url: ajax_call,
        data: str,
        dataType: "json",
        success: function(response){              
            $.each(response, function(key, value) {
                // after success submit data
                if(response){
                    var imagefile = $('#vImage').attr('src');
                    /* Image Upload Start */    
                    var ft = new FileTransfer();                     
                    var options = new FileUploadOptions();                      
                    options.fileKey="vImage1";                      
                    options.fileName=imagefile.substr(imagefile.lastIndexOf('/')+1);
                    options.mimeType="image/jpeg";  
                    options.mimeType="image/png"; 
                    var params = new Object();
                    params.value1 = "test";
                    params.value2 = "param";                       
                    options.params = params;
                    options.chunkedMode = false;                       
                    ft.upload(imagefile, your_service_url, win, fail, options);

                  }                                     
            });
        }
    });
    event.handled = true;
}
return false;
})

【讨论】:

  • 我怎样才能将此代码与其他表单元素(如姓名、性别、身高等)集成,以使用 ajax 或 json 将所有内容一起提交到我的服务器上的外部 php 页面insert mysql 查询会发生吗?
  • 我应该包含函数uploadImage()还是从提交事件开始?
  • html 文件结构如何??我试过了,但没有任何反应?
猜你喜欢
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
  • 2016-06-17
  • 1970-01-01
  • 2012-11-28
  • 2018-09-23
  • 2014-10-26
相关资源
最近更新 更多