与数据和图像提交使用PhoneGap的形式 [英] form with data and image submission using phonegap

查看:136
本文介绍了与数据和图像提交使用PhoneGap的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的PhoneGap,HTML,AJAX,jQuery的,MySQL和PHP。

在我Phonagep应用我有一个包含用户名,密码,userphoto形式。

我想从html页面提交此表。让说register.html

表单数据应该发送到在​​一个文件夹命名为服务上传我的服务器上的PHP页面可以说的路径是的 http://www.example.com/services/register.php

本页面应该处理MySQL的插入过程给也是上载在同一台服务器上的数据库。

在此服务器上,我也夹名为其中user形象应该是上传和全套卫浴设备应在MySQL数据库中插入图片。

我用ajax后的用户名和passowrd此页并插入数据succssfully都试过了。但我用序列化()时所面临的问题与数据。

我也可以用ajax不能上传图片?

我用这个code ..我得到响应的用户名和密码,而不发送图片的路径,但发送图片的路径,当什么也没发生。

 < HTML>
< HEAD>
    <冠军>基本上传< /标题>
    <脚本类型=文/ JavaScript的字符集=utf-8SRC =cordova.js>< / SCRIPT>
    <脚本类型=文/ JavaScript的SRC =HTTP://$c$c.jquery.com/jquery-latest.pack.js'>< / SCRIPT>
    <脚本类型=文/ JavaScript的字符集=utf-8>


功能分(E){

VAR英尺=新的文件传输();
VAR的选择=新FileUploadOptions();
options.fileKey =myImg;
。options.fileName = $('#myImg)VAL();
options.mimeType =为image / jpeg;
options.mimeType =图像/ PNG;
options.chunkedMode = FALSE;
VAR PATH = options.fileName;

VAR form_data =新的对象();
form_data ['用户名'] ='用户';
form_data ['密码'] ='1234';
form_data ['user_image_path'] =路径;

VAR URL =HTTP://localhost/stacktest/register.php;
$ .getJSON(+ URL +回调=+&放大器;?form_data =+ JSON.stringify(form_data)
    功能(响应){
        尝试 {
            //获取您的回应
            警报(响应);
        }
        赶上(前){
            执行console.log(前);
        }
   }
);

}
< / SCRIPT>
< /头>
<身体GT;

<形式方法=邮报是enctype =的multipart / form-data的ID =registerForm的onsubmit =子(THIS.VALUE);>

    <输入类型=文件名称=myImgID =myImg/>

    <输入类型=提交值=上传NAME =提交按钮ID =提交按钮/>


< /形式GT;
< /身体GT;
< / HTML>
 

解决方案

我觉得你的路径不正确,因为Web服务无法访问您的文件的路径,范围是你的应用程序中。上传您的文件阅读更多有关科尔多瓦文件传输

[更新]

您可以从 GitHub上

下载工作项目

阅读 PhonegapFileUpload博客

I'm using phonegap, html, ajax, jquery , mysql, and php.

In my Phonagep Application I have form that contain username, password, userphoto.

I want to submit this form from the html page. lets say register.html

form data should be sent to php page that is uploaded on my server in a folder named services lets say the path is "http://www.example.com/services/register.php".

this page should handle mysql insert process to a database that is also uploaded on the same server.

on this server I have also folder named images where user image should be uploaded and its full bath should be inserted in the mysql database.

I have tried using ajax to post user name and passowrd to this page and inserted data succssfully. but I face problem with the data when using serialize().

also I can't upload the image using ajax?

I used this code .. I get response for username and password without sending the image path but nothing happens when sending the image path

<html>
<head>
    <title>Basic Upload</title>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type='text/javascript' src='http://code.jquery.com/jquery-latest.pack.js'></script>
    <script type="text/javascript" charset="utf-8">


function sub(e){

var ft = new FileTransfer();   
var options = new FileUploadOptions();
options.fileKey="myImg";
options.fileName=$('#myImg').val();
options.mimeType="image/jpeg";  
options.mimeType="image/png"; 
options.chunkedMode = false;
var PATH = options.fileName;

var form_data= new Object();
form_data['user_name']= 'User';
form_data['password']= '1234';
form_data['user_image_path']= PATH;

var url = "http://localhost/stacktest/register.php"; 
$.getJSON(""+url+"?callback=?"+"&form_data="+JSON.stringify(form_data),
    function (response) {
        try {
            // Get Your Response
            alert(response);
        }
        catch(ex) {
            console.log(ex);
        }
   }
);

}
</script>
</head>
<body>

<form method="post" enctype="multipart/form-data" id="registerForm" onsubmit="sub(this.value);">

    <input type="file" name="myImg" id="myImg" />

    <input type="submit" value="Upload" name="submitButton" id="submitButton" />


</form>
</body>
</html>

解决方案

I think your path is incorrect because webservice can't access the path of your file which scope is within your app. to upload your file read more about Cordova File Transfer

[Update]

You can download the working project from GitHub

Read PhonegapFileUpload Blog

这篇关于与数据和图像提交使用PhoneGap的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆